#include <bits/stdc++.h>
#define ll int
using namespace std;
const ll Maxn=3e5+7,inf=1e9;
struct Treap{
ll son[2],val,cnt,sz,key;
}tree[Maxn];
ll tot,root;
void update(ll u){
tree[u].sz=tree[tree[u].son[0]].sz+tree[tree[u].son[1]].sz+1;
}
void rotate(ll &u,ll flg){
ll k=tree[u].son[flg];
tree[u].son[flg]=tree[k].son[flg^1];
tree[k].son[flg^1]=u;
tree[k].sz=tree[u].sz;
update(u),u=k;
}
void Insert(ll &u,ll x){
if(!u){
u=++tot;
tree[tot].sz=1;
tree[tot].val=x;
tree[tot].key=rand();
return ;
}
tree[u].sz++;
if(x>=tree[u].val) Insert(tree[u].son[1],x);
else Insert(tree[u].son[0],x);
if(tree[u].son[0]&&tree[u].key>tree[tree[u].son[0]].key) rotate(u,0);
if(tree[u].son[1]&&tree[u].key>tree[tree[u].son[1]].key) rotate(u,1);
update(u);
}
void Delete(ll &u,ll x){
tree[u].sz--;
if(tree[u].val==x){
if(tree[u].sz==1){
u=0;return ;
}
else if(!tree[u].son[0]||!tree[u].son[1]){
u=tree[u].son[0]+tree[u].son[1];
return ;
}
else{
if(tree[tree[u].son[0]].key<tree[tree[u].son[1]].key) rotate(u,0),Delete(tree[u].son[1],x);
else rotate(u,1),Delete(tree[u].son[0],x);
}
}
else{
if(tree[u].val>=x) Delete(tree[u].son[0],x);
else Delete(tree[u].son[1],x);
update(u);
}
}
ll GetRank(ll u,ll x){
if(x==tree[tree[u].son[0]].sz+1) return tree[u].val;
if(x>tree[tree[u].son[0]].sz+1) return GetRank(tree[u].son[1],x-tree[tree[u].son[0]].sz-1);
return GetRank(tree[u].son[0],x);
}
ll Getkth(ll u,ll x){
if(!u) return 0;
if(x>tree[u].val) return tree[tree[u].son[0]].sz+Getkth(tree[u].son[1],x)+1;
return Getkth(tree[u].son[0],x);
}
ll GetPre(ll u,ll x){
if(!u) return 0;
if(tree[u].val>=x) return GetPre(tree[u].son[0],x);
ll tmp=GetPre(tree[u].son[1],x);
return (tmp?tmp:tree[u].val);
}
ll GetSub(ll u,ll x){
if(!u) return 0;
if(tree[u].val<=x) return GetSub(tree[u].son[1],x);
ll tmp=GetSub(tree[u].son[0],x);
return (tmp?tmp:tree[u].val);
}
ll q;
int main(){
srand(19260817);
scanf("%d",&q);
while(q--){
ll opt,x;
scanf("%d%d",&opt,&x);
if(opt==1) Insert(root,x);
if(opt==2) Delete(root,x);
if(opt==3) printf("%d\n",Getkth(root,x)+1);
if(opt==4) printf("%d\n",GetRank(root,x));
if(opt==5) printf("%d\n",GetPre(root,x));
if(opt==6) printf("%d\n",GetSub(root,x));
}
return 0;
}