#include<bits/stdc++.h>
using namespace std;
const int maxn=0x7f7f7f7f;
struct node{
int val,lc,rc;
int rnd,size;
}t[200001];
int n;
int root,cnt;
void push_up(int x){
t[x].size=t[t[x].lc].size+t[t[x].rc].size+1;
}
int n_nod(int v){
int x=++cnt;
t[x]=(node){v,0,0,rand(),1};
return x;
}
void split(int now,int k,int &x,int &y){
if (!now){
x=0;
y=0;
return;
}
if (t[now].val<=k){
x=now;
split(t[now].rc,k,t[x].rc,y);
push_up(x);
}
else{
y=now;
split(t[now].lc,k,t[x].lc,y);
push_up(now);
}
}
int merge(int x,int y){
if (!x or !y){
return x^y;
}
if (t[x].rnd<t[y].rnd){
t[x].rc=merge(t[x].rc,y);
push_up(x);
return x;
}
else{
t[y].lc=merge(x,t[y].lc);
push_up(y);
return y;
}
}
void ins(int v){
int p,q;
split(root,v,p,q);
root=merge(merge(p,n_nod(v)),q);
return;
}
void delet(int v){
int p,q,r;
split(root,v,p,r);
split(p,v-1,p,q);
root=merge(merge(t[q].lc,t[q].rc),r);
return;
}
int get_rnk(int v){
int p,q,ans;
split(root,v-1,p,q);
ans=t[p].size+1;
root=merge(p,q);
return ans;
}
int get_k(int rt,int k){
int x=rt,rk=t[t[x].lc].size+1;
while (1){
if (rk==k) return t[x].val;
else if (rk>k) x=t[x].lc,rk-=t[t[x].rc].size+1;
else{
x=t[x].rc;
rk+=t[t[x].lc].size+1;
}
}
}
int get_pre(int x){
int p,q,ret;
split(root,x-1,p,q);
ret=get_k(p,t[p].size);
root=merge(p,q);
return ret;
ret=get_k(q,1);
root=merge(p,q);
return ret;
}
int get_suc(int x){
int p,q,ret;
split(root,x,p,q);
ret=get_k(q,1);
root=merge(p,q);
return ret;
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
srand(time(0));
cin>>n;
ins(-maxn);
ins(maxn);
while (n--){
int rs,rd;
cin>>rs>>rd;
if (rs==1) ins(rd);
if (rs==2) delet(rd);
if (rs==3) cout<<get_rnk(rd)-1<<endl;
if (rs==4) cout<<get_k(root,rd+1)<<endl;
if (rs==5) cout<<get_pre(rd)<<endl;
if (rs==6) cout<<get_suc(rd)<<endl;
}
return 0;
}