#include<bits/stdc++.h>
using namespace std;
struct tree{
int ls,rs,rank,key,siz,cnt;
}tr[100005];
int tot=0,n,root=0;
void Zig(int &x){
int y=tr[x].ls;
tr[x].ls=tr[y].rs;
tr[y].rs=x;
tr[y].siz=tr[x].siz;
tr[x].siz=tr[tr[x].ls].siz+tr[tr[x].rs].siz+tr[x].cnt;
x=y;
return;
}
void Zag(int &x){
int y=tr[x].rs;
tr[x].rs=tr[y].ls;
tr[y].ls=x;
tr[y].siz=tr[x].siz;
tr[x].siz=tr[tr[x].ls].siz+tr[tr[x].rs].siz+tr[x].cnt;
x=y;
return;
}
void Insert(int &now,int x){
if(!now){
now=++tot;
tr[now].key=x;
tr[now].rank=(rand()<<15)+rand();
tr[now].cnt=tr[now].siz=1;
tr[now].ls=tr[now].rs=0;
return;
}
if(x==tr[now].key){
tr[now].cnt++;
tr[now].siz++;
return;
}
else if(x<tr[now].key){
Insert(tr[now].ls,x);
if(tr[tr[now].ls].rank<tr[now].rank) Zig(now);
}
else{
Insert(tr[now].rs,x);
if(tr[tr[now].rs].rank<tr[now].rank) Zag(now);
}
tr[now].siz=tr[tr[now].ls].siz+tr[tr[now].rs].siz+tr[now].cnt;
}
void Delete(int &now,int x){
if(!now) return;
tr[now].siz--;
if(tr[now].key==x){
if(tr[now].cnt>1){
tr[now].cnt--;
return;
}
if(!tr[now].ls||!tr[now].rs) now=tr[now].ls+tr[now].rs;
else{
if(tr[tr[now].ls].rank<tr[tr[now].rs].rank){
Zig(now);
}
else Zag(now);
Delete(now,x);
}
return;
}
if(tr[now].key>x) Delete(tr[now].ls,x);
else Delete(tr[now].rs,x);
}
int findr(int now,int x){
if(now==0) return 0;
if(x>=tr[now].key) return tr[tr[now].ls].siz+tr[now].cnt+findr(tr[now].rs,x);
else return findr(tr[now].ls,x);
}
int findn(int now,int x){
if(tr[tr[now].ls].siz<x&&tr[tr[now].ls].siz+tr[now].cnt>=x) return tr[now].key;
if(tr[tr[now].ls].siz>=x) return findn(tr[now].ls,x);
else return findn(tr[now].rs,x-tr[tr[now].ls].siz-tr[now].cnt);
}
int findpre(int now,int x){
int val=0,id=now;
while(id){
if(tr[id].key<x){
val=tr[id].key;
id=tr[id].rs;
}
else id=tr[id].ls;
}
return val;
}
int findnxt(int now,int x){
int val=0,id=now;
while(id){
if(tr[id].key>x){
val=tr[id].key;
id=tr[id].ls;
}
else id=tr[id].rs;
}
return val;
}
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++){
int op,x;
scanf("%d%d",&op,&x);
if(op==1) Insert(root,x);
if(op==2) Delete(root,x);
if(op==3) printf("%d\n",findr(root,x));
if(op==4) printf("%d\n",findn(root,x));
if(op==5) printf("%d\n",findpre(root,x));
if(op==6) printf("%d\n",findnxt(root,x));
}
return 0;
}