#include <bits/stdc++.h>
using namespace std;
const int Mo=1e9+7;
#define lc(x) trp[x].ls
#define rc(x) trp[x].rs
int n,op,x,cn,root;
struct Node{
int ls,rs,key,pri,siz;
}trp[100010];
void split(int u,int x,int &L,int &R){
if(u==0){L=0;R=0;return;}
if(trp[u].key<=x){
L=u;
split(trp[u].rs,x,trp[u].rs,R);
}else{
R=u;
split(trp[u].ls,x,L,trp[u].ls);
}
trp[u].siz=trp[lc(u)].siz+trp[rc(u)].siz+1;
}
int merge(int L,int R){
if(L==0||R==0) return L+R;
if(trp[L].pri>trp[R].pri){
rc(L)=merge(rc(L),R);
trp[L].siz=trp[lc(L)].siz+trp[rc(L)].siz+1;
return L;
}else{
lc(R)=merge(L,lc(R));
trp[R].siz=trp[lc(R)].siz+trp[rc(R)].siz+1;
return R;
}
}
void Newnode(int x){
cn++;
trp[cn].ls=trp[cn].rs=0;
trp[cn].key=x;
trp[cn].pri=1LL*rand()*rand()%Mo;
trp[cn].siz=1;
}
void Insert(int x){
int L,R;
split(root,x,L,R);
Newnode(x);
int aa=merge(L,cn);
root=merge(aa,R);
}
void Erase(int x){
int L,R,p;
split(root,x,L,R);
split(root,x-1,L,p);
p=merge(lc(p),rc(p));
root=merge(merge(L,p),R);
}
int Rank(int x){
int L,R;
split(root,x-1,L,R);
int ans=trp[L].siz+1;
root=merge(L,R);
return ans;
}
int kth(int u,int x){
if(trp[lc(u)].siz+1==x) return trp[u].key;
if(x<=trp[lc(u)].siz) return kth(lc(u),x);
else return kth(rc(u),x-trp[lc(u)].siz-1);
}
int precursor(int x){
int L,R;
split(root,x-1,L,R);
int ans=kth(L,trp[L].siz);
root=merge(L,R);
return ans;
}
int succussor(int x){
int L,R;
split(root,x+1,L,R);
int ans=kth(R,1);
root=merge(L,R);
return ans;
}
int main(){
srand(time(0));
scanf("%d",&n);
while(n--){
scanf("%d%d",&op,&x);
if(op==1) Insert(x);
else if(op==2) Erase(x);
else if(op==3) printf("%d\n",Rank(x));
else if(op==4) printf("%d\n",kth(root,x));
else if(op==5) printf("%d\n",precursor(x));
else printf("%d\n",succussor(x));
}
return 0;
}