#include<bits/stdc++.h>
using namespace std;
int n;
int root,nt;
int op,x;
struct T_m{
int w,cnt;
int fa;
int s[2];
int siz;
void init(int W,int Fa){
w=W;cnt=1;fa=Fa;
s[0]=s[1]=0;
}
void cl(){
w=cnt=fa=s[0]=s[1]=0;
}
}T[1000010];
bool get(int x){
return T[T[x].fa].s[1]==x;
}
void clear(int x){
T[T[x].fa].s[get(x)]=0;
T[x].cl();
}
void calc(int x){
T[x].siz=T[x].cnt+T[T[x].s[0]].siz+T[T[x].s[1]].siz;
}
void rotate(int x){
int y=T[x].fa,z=T[y].fa,op=get(x);
T[z].s[get(y)]=x;
T[y].s[op]=T[x].s[op^1];
T[y].fa=x;
T[x].s[op^1]=y;
T[x].fa=z;
if(T[y].s[op])T[T[y].s[op]].fa=y;
calc(x);
calc(y);
if(z)calc(z);
}
void splay(int x,int fath){
int y=T[x].fa;
while(y!=fath){
if(T[y].fa!=fath){
if(get(x)==get(y))rotate(y);
else rotate(x);
}
rotate(x);
y=T[x].fa;
}
if(fath==0)root=x;
}
int find(int x){
int p=root,nx=T[p].s[x>T[p].w];
while(T[p].w!=x&&nx>2){
p=nx;
nx=T[p].s[x>T[p].w];
}
splay(p,0);
return p;
}
void insert(int x){
int p=root,ba=root;
while(T[p].w!=x&&p){
// printf("%d\n",p);
ba=p;
p=T[p].s[x>T[p].w];
}
if(p){
T[p].cnt++;
}else{
nt++;
T[nt].init(x,ba);
T[ba].s[x >T[ba].w] = nt;
p=nt;
// printf("add:%d at:%d fa=%d sonmode=%d\n",x,nt,ba,x >T[ba].w);
}
splay(p,0);
}
int pre(int x){
int p=find(x);
if(T[p].w<x)return p;
p=T[p].s[0];
while(T[p].s[1]){
p=T[p].s[1];
}
return p;
}
int suc(int x){
int p=find(x);
if(T[p].w>x)return p;
p=T[p].s[1];
while(T[p].s[0]){
p=T[p].s[0];
}
// printf("for %d find %d\n",x,p);
return p;
}
void dele(int x){
int n_p=pre(x),n_b=suc(x),sel;
// printf("%d %d\n",n_p,n_b);
splay(n_p,0);
splay(n_b,n_p);
sel=T[n_b].s[0];
T[sel].cnt--;
if(T[sel].cnt==0){
clear(sel);
return;
}
splay(n_b,0);
}
int get_num(int x){
int p=root;
while(x&&p){
if(x>T[T[p].s[0]].siz){
p=T[p].s[1];
x-=T[T[p].s[0]].siz;
}else{
if(x==T[T[p].s[0]].siz)return T[p].w;
p=T[p].s[1];
}
}
splay(p,0);
return 0;
}
int get_rank(int x){
splay(find(x),0);
return T[T[root].s[0]].siz;
}
int main(){
// freopen("tree.in","r",stdin);
// freopen("tree.out","w",stdout);
insert(2e9+5);
insert(-(2e9+5));
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d %d",&op,&x);
switch(op){
case 1:insert(x);break;
case 2:dele(x);break;
case 3:printf("%d\n",get_rank(x));break;
case 4:printf("%d\n",get_num(x));break;
case 5:printf("%d\n",T[pre(x)].w);break;
case 6:printf("%d\n",T[suc(x)].w);break;
}
}
return 0;
}