#include<cstdio>
#include<cstring>
#include<iostream>
#include<cstdlib>
using namespace std;
const int MAX=0x7fffffff;
const int MIN=-19999999;
struct node{
int key,pri,siz,cnt;
node *ch[2];
node(){}
node(int x,int y,int z,int w){
key=x;pri=y;siz=z;cnt=w;
}
void rs(){siz=ch[0]->siz+ch[1]->siz+cnt;}
};
typedef node *pnode;
pnode null,root;
/*
d=1:Zag
d=0:Zig
*/
void rotate(pnode &t,bool d){
pnode tmp=t->ch[d];t->ch[d]=tmp->ch[!d];tmp->ch[!d]=t;
tmp->siz=t->siz;
t->rs();
t=tmp;
}
void ins(pnode &t,int key){
if(t==null){
t=new struct node(key,rand(),1,1);
t->ch[0]=t->ch[1]=null;
}
else if(key==t->key)t->cnt++;
else{
bool d=key > t->key;
ins(t->ch[d],key);
if(t->ch[d]->pri < t->pri)rotate(t,d);
}
t->rs();
}
void del(pnode &t,int key){
if(t==null)return;
else if(key==t->key){
if(t->cnt>1)t->cnt--;
else if(t->ch[0]==null && t->ch[1]==null){
delete t;t=null;
}
else if(t->ch[0]==null)t=t->ch[1];
else if(t->ch[1]==null)t=t->ch[0];
else{
rotate(t,t->ch[0]->pri > t->ch[1]->pri);
del(t,key);
}
}else del(t->ch[key > t->key],key);
t->rs();
}
void order(pnode t){
if(t==null)return;
order(t->ch[0]);
for(int i=1;i<=t->cnt;i++)printf("%d ",t->key);
order(t->ch[1]);
}
int quer_no(pnode t,int key){
if(t==null)return 0;
if(t->key==key)return t->ch[0]->siz+1;
if(t->key > key)return quer_no(t->ch[0],key);
return t->ch[0]->siz+t->cnt+quer_no(t->ch[1],key);
}
int find_k(pnode t,int k){
if(t->siz < k)return 0;
if(t->ch[0]->siz<k&&t->cnt+t->ch[0]->siz>=k)return t->key;
if(t->ch[0]->siz>=k)return find_k(t->ch[0],k);
return find_k(t->ch[1],k-t->ch[0]->siz-t->cnt);
}
int pred(pnode t,int key){
if(t==null)return MIN;
if(key<=t->key)return pred(t->ch[0],key);
return max(t->key,pred(t->ch[1],key));
}
int succ(pnode t,int key){
if(t==null)return MAX;
if(key>=t->key)return succ(t->ch[1],key);
return min(t->key,succ(t->ch[0],key));
}
int main(){
/*init*/
null=new struct node(0,MAX,0,0);
null->ch[0]=null->ch[1]=null;
root=null;
srand(121);
int fg,x,n;
scanf("%d",&n);
while(n--){
scanf("%d%d",&fg,&x);
switch(fg){
case 1:
ins(root,x);
break;
case 2:
del(root,x);
break;
case 3:
printf("%d\n",quer_no(root,x));
break;
case 4:
printf("%d\n",find_k(root,x));
break;
case 5:
printf("%d\n",pred(root,x));
break;
case 6:
printf("%d\n",succ(root,x));
break;
}
}