#include<iostream>
#include<cstdio>
#define cin(x) scanf("%d",&x)
using namespace std;
int ch[][],key[],
cnt[],size[],sz,rt,f[];
bool get(int x)
{
return ch[f[x]][]==x;
}
void clear(int x)
{
f[x]=cnt[x]=ch[x][]=ch[x][]=size[x]=key[x]=;
}
void pushup(int x)
{
if(x)
{
size[x]=cnt[x];
if(ch[x][])size[x]+=size[ch[x][]];
if(ch[x][])size[x]+=size[ch[x][]];
}
}
void rotate(int x)
{
int old=f[x],oldf=f[old],which=get(x);
ch[old][which]=ch[x][which^];f[ch[old][which]]=old;
ch[x][which^]=old;f[old]=x;
f[x]=oldf;
if(oldf)ch[oldf][ch[oldf][]==old]=x;
pushup(old),pushup(x);
}
void splay(int x)
{
for(int fa;fa=f[x];rotate(x))
if(f[fa])
rotate(get(x)==get(fa)?fa:x);
rt=x;
}
void insert(int x)
{
if(rt==)
{
sz++;key[sz]=x;rt=sz;
cnt[sz]=size[sz]=;
f[sz]=ch[sz][]=ch[sz][]=;
return;
}
int now=rt,fa=;
while()
{
if(x==key[now])
{
cnt[now]++;
pushup(now);
pushup(fa);
splay(now);
return;
}
fa=now;
now=ch[now][x>key[now]];
if(now==)
{
sz++;
size[sz]=cnt[sz]=;
ch[sz][]=ch[sz][]=;
ch[fa][x>key[fa]]=sz;
f[sz]=fa;
key[sz]=x;
pushup(fa);
splay(sz);
return;
}
}
}
int rnk(int x)
{
int now=rt,ans=;
while()
{
if(ch[now][] && x<key[now])now=ch[now][];
else
{
ans+=size[ch[now][]];
if(x==key[now])
{
splay(now);
return ans+;
}
ans+=cnt[now];
now=ch[now][];
}
}
}
int kth(int x)
{
int now=rt;
while()
{
if(ch[now][] && x<=size[ch[now][]])now=ch[now][];
else
{
int temp=size[ch[now][]]+cnt[now];
if(temp>=x)
return key[now];
x-=temp;now=ch[now][];
}
}
}
int pre()
{
int now=ch[rt][];
while(ch[now][])now=ch[now][];
return now;
}
int next()
{
int now=ch[rt][];
while(ch[now][])now=ch[now][];
return now;
}
void del(int x)
{
rnk(x);
if(cnt[rt]>){cnt[rt]--;pushup(rt);return;}
if(!ch[rt][] && !ch[rt][]){clear(rt);rt=;return;}
if(!ch[rt][])
{
int old=rt;rt=ch[rt][];f[rt]=;clear(old);return;
}
else if(!ch[rt][])
{
int old=rt;rt=ch[rt][];f[rt]=;clear(old);return;
}
int oldrt=rt,leftbig=pre();
splay(leftbig);//leftbig无儿子,所以oldrt无左二子
ch[rt][]=ch[oldrt][];
f[ch[oldrt][]]=rt;
clear(oldrt);
pushup(rt); }
signed main()
{
// freopen("input5.in","r",stdin); int n,opt,x;
cin(n);
while(n--)
{
cin(opt);cin(x);
if(opt==)insert(x);
if(opt==)del(x);
if(opt==)printf("%ld\n",rnk(x));
if(opt==)printf("%ld\n",kth(x));
if(opt==){insert(x);printf("%ld\n",key[pre()]);del(x);}
if(opt==){insert(x);printf("%ld\n",key[next()]);del(x);}
}
}