# 使用有序列表来维护集合
import bisect

M = []

def insertValue(x):
    # TODO: 实现插入逻辑
    pos = bisect.bisect_left(M, x)
    # 检查是否已存在
    if pos >= len(M) or M[pos] != x:
        M.insert(pos, x)

def eraseValue(x):
    # TODO: 实现删除逻辑
    pos = bisect.bisect_left(M, x)
    if pos < len(M) and M[pos] == x:
        M.pop(pos)

def xInSet(x):
    # TODO: 实现存在性检查
    pos = bisect.bisect_left(M, x)
    return pos < len(M) and M[pos] == x

def sizeOfSet():
    # TODO: 返回集合大小
    return len(M)
    

def getPre(x):
    # TODO: 实现找前驱
    # 查询集合M中小于x且最大的数(前驱)
    # 使用二分查找找到插入位置
    pos = bisect.bisect_left(M, x)
    if pos > 0:
        return M[pos - 1]
    else:
        return -1
    

def getBack(x):
    # TODO: 实现找后继
    # 查询集合M中大于x且最小的数(后继)
    # 使用二分查找找到插入位置
    pos = bisect.bisect_right(M, x)
    if pos < len(M):
        return M[pos]
    else:
        return -1

def main():
    q = int(input())
    for _ in range(q):
        line = map(int,input().split())
        cnt,op,x=0,0,0
        for i in line:
            if(cnt==0):
                op=i
            else:
                x=i
            cnt+=1
        
        if op == 1:
            insertValue(x)
        elif op == 2:
            eraseValue(x)
        elif op == 3:
            print("YES" if xInSet(x) else "NO")
        elif op == 4:
            print(sizeOfSet())
        elif op == 5:
            print(getPre(x))
        elif op == 6:
            print(getBack(x))

if __name__ == "__main__":
    main()