import sys
sys.setrecursionlimit(60000)
class Node():
def __init__(self, data=None):
self.data = data
self.lchild = None
self.rchild = None
self.cnt = 0
self.siz = 0
class BSTree():
def __init__(self):
self.root = None
self.count = 0
self.max = Node(-2147483647)
self.min = Node(2147483647)
def insert(self, node, val):
self.count += 1
if not node:
node = Node(val)
node.siz = self.count
self.count = 0
elif val < node.data:
node.lchild = self.insert(node.lchild, val)
elif val > node.data:
node.rchild = self.insert(node.rchild, val)
elif val == node.data:
node.cnt += 1
return node
def prior(self, val):
max = self.max
p = self.root
while p is not None:
if p.data >= val:
p = p.lchild
else:
max = p
p = p.rchild
return max
def next(self, val):
min = self.min
p = self.root
while p is not None:
if p.data <= val:
p = p.rchild
else:
min = p
p = p.lchild
return min
def find_by_rank(self, node, val):
if node.siz <= val <= node.siz + node.cnt:
return node.data
elif val < node.siz:
return self.find_by_rank(node.lchild, val)
elif val > node.siz + node.cnt:
return self.find_by_rank(node.rchild, val)
q = eval(input())
tree = BSTree()
for i in range(q):
op, x = [int(i) for i in input().strip().split()]
if op == 1:
node = tree.prior(x)
print(node.siz+node.cnt+1)
elif op == 2:
print(tree.find_by_rank(tree.root,x))
elif op == 3:
print(tree.prior(x).data)
elif op == 4:
print(tree.next(x).data)
elif op == 5:
tree.root = tree.insert(tree.root, x)