#include<bits/stdc++.h>
using namespace std;
int t,op,cnt,rt;
struct node{
int msiz,tsiz,val,ch[2],f;
}tree[1000010];
void maintain(int o){
if(!o)return;
tree[o].tsiz=tree[o].msiz;
if(tree[o].ch[0])tree[o].tsiz+=tree[tree[o].ch[0]].tsiz;
if(tree[o].ch[1])tree[o].tsiz+=tree[tree[o].ch[1]].tsiz;
}
int get(int o){
return tree[tree[o].f].ch[1]==o;
}
void clear(int o){
tree[o].ch[0]=tree[o].ch[1]=tree[o].f=tree[o].msiz=tree[o].tsiz=0;
}
int find_pre(){
int o=tree[rt].ch[0];
while(tree[o].ch[1])o=tree[o].ch[1];
return tree[o].val;
}
int find_nxt(){
int o=tree[o].ch[1];
while(tree[o].ch[0])o=tree[o].ch[0];
return tree[o].val;
}
void rotate(int o){
int fa=tree[o].f,ffa=tree[fa].f,pos=get(o);
tree[fa].ch[pos]=tree[o].ch[pos^1];
tree[tree[fa].ch[pos]].f=fa;
tree[o].ch[pos^1]=fa;
tree[fa].f=o;
tree[o].f=ffa;
if(ffa)tree[ffa].ch[tree[ffa].ch[1]==fa]=o;
maintain(fa);
maintain(o);
}
void splay(int o){
for(int fa;fa=tree[o].f;rotate(o))
if(tree[fa].f)rotate((get(o)==get(fa))?fa:o);
rt=o;
}
void insert(int x){
if(!rt){
cnt++;
rt=cnt;
tree[cnt].ch[0]=tree[cnt].ch[1]=0;
tree[cnt].f=0;
tree[cnt].msiz=tree[cnt].tsiz=1;
tree[cnt].val=x;
return;
}
int o=rt,fa=0;
while(1){
if(x==tree[o].val){
tree[o].msiz++;
maintain(o);
maintain(fa);
splay(o);
break;
}
fa=o;
o=tree[o].ch[x>tree[o].val];
if(!o){
cnt++;
tree[cnt].ch[0]=tree[cnt].ch[1]=0;
tree[cnt].msiz=tree[cnt].tsiz=1;
tree[cnt].val=x;
tree[cnt].f=fa;
tree[fa].ch[tree[fa].val<x]=cnt;
maintain(fa);
splay(cnt);
break;
}
}
}
int find_rank(int x){
int ans=0,o=rt;
while(1){
if(x<tree[o].val)o=tree[o].ch[0];
else{
if(tree[o].ch[0])ans+=tree[tree[o].ch[0]].tsiz;
if(x==tree[o].val){
splay(o);
return ans+1;
}
ans+=tree[o].msiz;
o=tree[o].ch[1];
}
}
}
void remove(int x){
int r=find_rank(x);
if(tree[rt].msiz>1){
tree[rt].msiz--;
maintain(rt);
return;
}
if(!tree[rt].ch[0]&&!tree[rt].ch[1]){
clear(rt);
rt=0;
}
else if(!tree[rt].ch[0]){
int xx=rt;
rt=tree[rt].ch[1];
tree[rt].f=0;
clear(xx);
}
else if(!tree[rt].ch[1]){
int xx=rt;
rt=tree[rt].ch[0];
tree[rt].f=0;
clear(xx);
}
else{
int pos=find_pre(),xx=rt;
splay(pos);
tree[rt].ch[1]=tree[xx].ch[1];
tree[tree[xx].ch[1]].f=rt;
clear(xx);
maintain(rt);
}
}
int find_val(int x){
int o=rt;
while(1){
if(tree[o].ch[0]&&tree[tree[o].ch[0]].tsiz>=x)x=tree[o].ch[0];
else{
int s=(tree[o].ch[0]?tree[tree[o].ch[0]].tsiz:0)+tree[o].msiz;
if(x<=s)return tree[o].val;
x-=s;
o=tree[o].ch[1];
}
}
}
int main(){
scanf("%d",&t);
while(t--){
scanf("%d",&op);
if(op==1){
int x;
scanf("%d",&x);
insert(x);
}
if(op==2){
int x;
scanf("%d",&x);
remove(x);
}
if(op==3){
int x;
scanf("%d",&x);
insert(x);
printf("%d\n",find_rank(x));
remove(x);
}
if(op==4){
int x;
scanf("%d",&x);
printf("%d\n",find_val(x));
}
if(op==5){
int x;
scanf("%d",&x);
insert(x);
printf("%d\n",find_pre());
remove(x);
}
if(op==6){
int x;
scanf("%d",&x);
insert(x);
printf("%d\n",find_nxt());
remove(x);
}
}
return 0;
}