#include<bits/stdc++.h>
using namespace std;
struct node
{
int val,cnt,l,r,size,dat;
}t[114514];
int tot,root;
int New(int w)
{
t[++tot].val=w;
t[tot].dat=rand();
t[tot].size=t[tot].cnt=1;
return tot;
}
void update(int p)
{
t[p].size=t[t[p].l].size+t[t[p].r].size+t[p].cnt;
}
void zig(int &p)
{
int q=t[p].l;
t[p].l=t[q].r,t[q].r=p;
p=q;
update(t[p].r),update(p);
}
void zag(int &p)
{
int q=t[p].r;
t[p].r=t[q].l,t[q].l=p;
p=q;
update(t[p].l),update(p);
}
void insert(int &p,int w)
{
if(!p)
{
p=New(w);
return;
}
if(w==t[p].val)
{
t[p].cnt++;
update(p);
return;
}
if(w<t[p].val)
{
insert(t[p].l,w);
if(t[p].dat<t[t[p].l].dat)
zig(p);
}
else
{
insert(t[p].r,w);
if(t[p].dat<t[t[p].r].dat)
zag(p);
}
update(p);
}
void del(int &p,int w)
{
if(!p)
return;
if(w==t[p].val)
{
if(t[p].cnt>1)
{
t[p].cnt--;
update(p);
return;
}
if(t[p].l||t[p].r)
{
if(!t[p].r||t[t[p].l].dat>t[t[p].r].dat)
{
zag(p);
del(t[p].r,w);
}
else
{
zig(p);
del(t[p].l,w);
}
update(p);
}
else
p=0;
return;
}
w<t[p].val?del(t[p].l,w):del(t[p].r,w);
update(p);
}
int get_rank(int p,int w)
{
if(!p)
return 0;
if(w==t[p].val)
return t[t[p].l].size+1;
else if(w<t[p].val)
return get_rank(t[p].l,w);
else
return t[t[p].l].size+t[p].cnt+(t[p].r,w);
}
int get_val(int p,int rank)
{
if(!p)
return 1e9;
if(rank<=t[t[p].l].size)
return get_val(t[p].l,rank);
else if(rank<=t[t[p].l].size+t[p].cnt)
return t[p].val;
else
return get_val(t[p].r,rank-t[t[p].l].size-t[p].cnt);
}
int get_pre(int w)
{
int p=root,pre;
while(p)
{
if(t[p].val<w)
pre=t[p].val,p=t[p].r;
else
p=t[p].l;
}
return pre;
}
int get_nxt(int w)
{
int p=root,nxt;
while(p)
{
if(t[p].val>w)
nxt=t[p].val,p=t[p].l;
else
p=t[p].r;
}
return nxt;
}
int n;
int main()
{
cin>>n;
while(n--)
{
int op,x;
cin>>op>>x;
if(op==1)
insert(root,x);
else if(op==2)
del(root,x);
else if(op==3)
cout<<get_rank(root,x)<<endl;
else if(op==4)
cout<<get_val(root,x)<<endl;
else if(op==5)
cout<<get_pre(x)<<endl;
else
cout<<get_nxt(x)<<endl;
}
}