#include<bits/stdc++.h>
inline void read(int &a)
{
a=0;int b=1;char c=getchar();
while(c<'0'||c>'9')
{
if(c=='-')b=-1;
c=getchar();
}
while(c>='0'&&c<='9')
{
a=(a<<1)+(a<<3)+c-'0';
c=getchar();
}
a*=b;
}
inline void write(int a)
{
if(a<0){a=-a;putchar('-');}
if(a>9)write(a/10);
putchar(a%10+'0');
}
const int maxn=100001;
int nodenum=0,root=0;
struct node
{
int order,num,sum,repeat;
int son[2];
inline int comp(int x)
{
if(num==x)return -1;
if(num>x)return 0;
return 1;
}
inline bool operator>(const node&b)
{
if(order>b.order)return true;
return false;
}
}p[maxn<<1];
inline void newnode(int x)
{
p[++nodenum].num=x;
p[nodenum].order=rand()*rand();
p[nodenum].son[0]=p[nodenum].son[1]=0;
p[nodenum].repeat=1;
}
inline void update(int k)
{
p[k].sum=p[p[k].son[0]].sum+p[p[k].son[1]].sum+p[k].repeat;
}
inline void rotate(int &k,int d)
{
int t=p[k].son[d];
p[k].son[d]=p[t].son[d^1];
p[t].son[d^1]=k;
update(k);update(t);
k=t;
}
void insert(int &k,int x)
{
if(!k)
{
newnode(x);
k=nodenum;
return ;
}
int d=p[k].comp(x);
if(d==-1){++p[k].repeat;return ;}
insert(p[k].son[d],x);
if(p[p[k].son[d]]>p[k])rotate(k,d);
else update(k);
}
void remove(int &k,int x)
{
int d=p[k].comp(x);
if(d==-1)
{
if(p[k].repeat>1){--p[k].repeat;return ;}
if(!(p[k].son[0])&&!(p[k].son[1])){k=0;return ;}
if(!(p[k].son[0]*p[k].son[1]))
{
if(p[k].son[0]){k=p[k].son[0];return ;}
else{k=p[k].son[1];return ;}
}
else
{
int r;
if(p[p[k].son[0]]>p[p[k].son[1]])r=0;
else r=1;
rotate(k,r);
insert(k,r^1);
update(k);
}
}
}
int rank(int &k,int x,int has)
{
int d=p[k].comp(x);
if(d==-1)return has+p[p[k].son[0]].sum+1;
if(d==0)return rank(p[k].son[0],x,has);
if(d==1)return rank(p[k].son[1],x,has+p[p[k].son[0]].sum+p[k].repeat);
}
int kth(int &k,int x,int has)
{
if(x>=has+p[p[k].son[0]].sum+1&&x<=has+p[p[k].son[0]].sum+p[k].repeat)return p[k].num;
if(x<=has+p[p[k].son[0]].sum)return kth(p[k].son[0],x,has);
else return kth(p[k].son[1],x,has+p[p[k].son[0]].sum+p[k].repeat);
}
int main()
{
//freopen("test1.txt","r",stdin);
srand(time(NULL));
p[0].son[0]=p[0].son[1]=p[0].num=p[0].order=p[0].repeat=0;
int m,opt,x;
read(m);
while(m--)
{
read(opt);read(x);
if(opt==1)insert(root,x);
if(opt==2)remove(root,x);
//if(opt==4)write(rank(root,x,0));
if(opt==3)write(kth(root,x,0));
if(opt==5)write(kth(root,rank(root,x,0)-1,0));
if(opt==6)write(kth(root,rank(root,x,0)+1,0));
if(opt>=3)printf("\n");
}
return 0;
}