#include <bits/stdc++.h>
#define ls t[x].s[0]
#define rs t[x].s[1]
using namespace std;
const int N = 1e7+10;
int n;
int rt,tot;
struct node{
int val,sz,s[2],cnt,fa;
}t[N];
void pushup(int x){
t[x].sz=t[ls].sz+t[rs].sz+t[x].cnt;
}
inline bool get(int x){
return t[t[x].fa].s[1]==x;
}
inline void rotate(int x){
int f=t[x].fa,g=t[f].fa,c=get(x);
if(g) t[g].s[get(f)]=x;
t[t[x].s[c^1]].fa=f;
t[f].s[c]=t[x].s[c^1];
t[f].fa=x;
t[x].fa=g;
t[x].s[c^1]=f;
pushup(f),pushup(x);
}
void splay(int x,int goal){
for(int y;y=t[x].fa,t[x].fa!=goal;rotate(x)){
if(t[y].fa!=goal) rotate(get(x)==get(y)?y:x);
}
if(!goal) rt=x;
}
void insert(int val){
int x=rt,f=0;
while(x&&t[x].val!=val){
f=x,x=t[x].s[t[x].val<val];
}
if(x) t[x].cnt++;
else{
x=++tot;
if(f) t[f].s[t[f].val<val]=tot;
t[x].cnt=t[tot].sz=1;
t[x].val=val;
t[x].fa=f;
}
splay(x,0);
}
int find(int k){
int x=rt;
while(1){
if(t[ls].sz>=k) x=ls;
else if(t[ls].sz+t[x].cnt<k) k-=t[ls].sz+t[x].cnt,x=rs;
else return t[x].val;
}
}
int where(int val){
int x=rt;
while(t[x].s[t[x].val<val]&&t[x].val!=val){
x=t[x].s[t[x].val<val];
}
return x;
}
int get_rank(int val){
int x=where(val);
splay(x,0);
return t[t[rt].s[0]].sz;
}
int get_pre(int val){
int x=where(val);
splay(x,0);
if(t[x].val<val) return x;
x=ls;
while(rs) x=rs;
return x;
}
int get_next(int val){
int x=where(val);
splay(x,0);
if(t[x].val>val) return x;
x=rs;
while(ls) x=ls;
return x;
}
void del(int val)
{
int pre=get_pre(val),ne=get_next(val);
splay(pre,0),splay(ne,pre);
int tmp=t[ne].s[0];
if(t[tmp].cnt) {
t[tmp].cnt--;
if(!t[tmp].cnt) t[ne].s[0]=0;
else splay(tmp,0);
}
}
int main()
{
insert(1e9),insert(-1e9);
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
int op,x;
scanf("%d%d",&op,&x);
if(op==1)
insert(x);
if(op==2)
del(x);
if(op==3)
printf("%d\n",get_rank(x));
if(op==4)
printf("%d\n",find(x+1));
if(op==5)
printf("%d\n",t[get_pre(x)].val);
if(op==6)
printf("%d\n",t[get_next(x)].val);
}
return 0;
}