#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=100005;
int n,m,root,idx;
struct node
{
int s[2];
int p;
int size;
int cnt;
int v;
void init(int p1,int v1)
{
p=p1;
v=v1;
cnt=size=1;
}
}t[N];
void pushup(int x)
{
t[x].size=t[t[x].s[1]].size+t[t[x].s[0]].size+t[x].cnt;
}
void rotate(int x)
{
int y=t[x].p,z=t[y].p;
int k=t[y].s[1]==x;
t[y].s[k]=t[x].s[k^1];
t[t[x].s[k^1]].p=y;
t[x].s[k^1]=y;
t[y].p=x;
t[z].s[t[z].s[1]==y]=x;
t[x].p=z;
pushup(y),pushup(x);
}
void splay(int x,int k)
{
while(t[x].p!=k)
{
int y=t[x].p,z=t[y].p;
if(z!=k)
{
if((t[y].s[0]==x)^t[z].s[1]==y)
rotate(x);
else
rotate(y);
}
rotate(x);
}
if(k==0)
root=x;
}
void find(int v)
{
int x=root;
while(t[x].v!=v&&t[x].s[v>t[x].v])
{
x=t[x].s[v>t[x].v];
}
splay(x,0);
}
int getpre(int v)
{
find(v);
int x=root;
if(t[x].v<v)
return x;
x=t[x].s[0];
while(t[x].s[1])
x=t[x].s[1];
return x;
}
int getsuc(int v)
{
find(v);
int x=root;
if(t[x].v>v)
return x;
x=t[x].s[1];
while(t[x].s[0])
x=t[x].s[0];
return x;
}
void del(int v)
{
int pre=getpre(v);
int suc=getsuc(v);
splay(pre,0),splay(suc,pre);
int res=t[suc].s[0];
if(t[res].cnt>1)
t[res].cnt--,splay(res,0);
else
t[suc].s[0]=0,splay(suc,0);
}
int getrank(int v)
{
find(v);
return t[t[root].s[0]].size;
}
int getval(int k)
{
int x=root;
while(1)
{
int y=t[x].s[0];
if(t[y].size+t[x].cnt<k)
{
k-=t[y].size+t[x].cnt;
x=t[x].s[1];
}
else
{
if(t[y].size>=k)
x=t[x].s[0];
else
break;
}
}
splay(x,0);
return t[x].v;
}
void insert(int v)
{
int x=root,p=0;
while(x && t[x].v!=v)
{
p=x;
x=t[x].s[v>t[x].v];
}
if(x)
t[x].cnt++;
else
{
x=++idx;
t[p].s[v>t[p].v]=x;
t[x].init(p,v);
}
splay(x,0);
}
signed main()
{
insert(-1e9);insert(1e9);
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
int op,x;
scanf("%d%d",&op,&x);
if(op==1) insert(x);
if(op==2) del(x);
if(op==3) printf("%d\n",getrank(x));
if(op==4) printf("%d\n",getval(x+1));
if(op==5) printf("%d\n",t[getpre(x)].v);
if(op==6) printf("%d\n",t[getsuc(x)].v);
}
return 0;
}