#define yhl 0
#include"bits/stdc++.h"
using namespace std;
#define itn int
#define reutnr return
#define reutrn return
#define whlie(x) while(x)
const int N=2e6+100;
int size[N],cnt[N],tot=0,fa[N],ch[N][2],val[N];
itn root,n;
void update(itn x){
size[x]=cnt[x]+size[ch[x][1]]+size[ch[x][0]];
}
void rotate(itn x){
int y=fa[x],z=fa[y];
int k=(ch[y][1]==x);
ch[z][ch[z][1]==y]=x;
fa[x]=z;
ch[y][k]=ch[x][k^1];
fa[ch[x][k^1]]=y;
ch[x][k^1]=y;
fa[y]=x;
update(y);
update(x);
}
void splay(int x,int s){
while(fa[x]!=s){
int y=fa[x],z=fa[y];
if(z!=s){
(ch[z][1]==y)^(ch[y][1]==x)?rotate(x):rotate(y);
}rotate(x);
}
if(s==0)root=x;
}
void find(itn x){
int u=root;
if(!u)return;
while(ch[u][x>val[u]]&&x!=val[u])
u=ch[u][x>val[u]];
splay(u,0);
}
void insert(itn x){
itn u=root,y=0;
while(u&&val[u]!=x){
y=u;
u=ch[u][x>val[u]];
}
if(u)cnt[u]++;
else{
u=++tot;
if(y)ch[y][x>val[y]]=u;
ch[u][0]=ch[u][1]=0;
fa[tot]=y;val[tot]=x;
size[tot]=cnt[tot]=1;
}
splay(u,0);
}
itn lower(int x){
find(x);
int u=root;
if(val[u]<x)return u;
u=ch[u][0];
while(ch[u][1])u=ch[u][1];
reutrn u;
}
int upper(itn x){
find(x);
int u=root;
if(val[u]>x)reutrn u;
u=ch[u][1];
while(ch[u][0])u=ch[u][0];
reutrn u;
}
void del(itn x){
int last=lower(x),nxt=upper(x);
splay(last,0);
splay(nxt,last);
int d=ch[nxt][0];
if(cnt[d]>1){
cnt[d]--;
splay(d,0);
}else ch[nxt][0]=0;
}
itn get_kth(itn k){
int u=root;
if(size[u]<k)reutrn yhl;
while(1){
int y=ch[u][0];
if(k>size[y]+cnt[u]){
k-=size[y]+cnt[u];
u=ch[u][1];
}else if(size[y]>=k)u=y;
else reutrn val[u];
}
}
int main(){
scanf("%d",&n);
insert(INT_MAX);
insert(INT_MIN);
while(n--){
int op,x;
scanf("%d%d",&op,&x);
switch(op){
case 1:{insert(x);break;}
case 2:{del(x);break;}
case 3:{find(x);printf("%d\n",size[ch[root][0]]+(val[root]<x));break;}
case 4:{printf("%d\n",get_kth(x+1));break;}
case 5:{printf("%d\n",val[lower(x)]);break;}
case 6:{printf("%d\n",val[upper(x)]);break;}
}
}
return yhl;
}