#include<bits/stdc++.h>
using namespace std;
struct node{
node *l,*r;
int val,siz;
int rsiz;
bool del;
node(){
l=r=NULL;
val=siz=rsiz=0;
del=1;
}
};
vector<node*>t;
struct scape_goat{
node *root,*null;
scape_goat(){
root=new node;
null=root;
null->l=null->r=null;
}
void update(node *p){
p->siz=p->l->siz+p->r->siz+1;
p->rsiz=p->rsiz+p->r->rsiz+!p->del;
}
bool check(node *p){
if(p->del) return false;
if(0.75*p->siz<=double(max(p->l->siz,p->r->siz)))
return true;
if(double(p->rsiz)<=0.75*p->siz)
return true;
return false;
}
void dfs(node *p){
if(p==null) return;
dfs(p->l);
if(!p->del) t.push_back(p);
dfs(p->r);
}
node *build(int l,int r){
if(l>=r) return null;
int mid=l+r>>1;
t[mid]->l=build(l,mid);
t[mid]->r=build(mid+1,r);
update(t[mid]);
return t[mid];
}
void recon(node *&p){
t.clear();
dfs(p);
p=build(0,t.size());
}
void insert(int val,node *&p){
if(p==null){
p=new node;
p->val=val;
p->l=p->r=null;
p->siz=p->rsiz=1;
p->del=0;
return;
}
else if(p->val>val) insert(val,p->l);
else insert(val,p->r);
update(p);
if(check(p)) recon(p);
return;
}
void erase(int x,node *p){
if(!p->del && x==p->val) {
p->del=1;
update(p);
return;
}
update(p);
if(x<p->val) erase(x,p->l);
else erase(x,p->r);
}
int ranks(int x){
node *p=root;
int ans=1;
while(p!=null)
if(p->val>=x) p=p->l;
else ans+=p->l->rsiz+!p->del,p=p->r;
return ans;
}
int get(int x){
node *p=root;
while(p!=null){
if(!p->del && p->l->rsiz+1==x) return p->val;
if(p->l->rsiz>=x) p=p->l;
else{
x-=p->l->rsiz+!p->del;
p=p->r;
}
}
}
int pre_find(int x){
return get(ranks(x)-1);
}
int next_find(int x){
return get(ranks(x+1));
}
}p;
int main() {
int u;
cin>>u;
while(u--){
int opt,x;
cin>>opt>>x;
if(opt==1) p.insert(x,p.root);
if(opt==2) p.erase(x,p.root);
if(opt==3) cout<<p.ranks(x)<<"\n";
if(opt==4) cout<<p.get(x)<<"\n";
if(opt==5) cout<<p.pre_find(x)<<"\n";
if(opt==6) cout<<p.next_find(x)<<"\n";
}
return 0;
}