#include<bits/stdc++.h>
using namespace std;
struct node
{
int val,rnd,size;
node *l,*r;
};
node *root=NULL;
void update(node *p)
{
if(p==NULL)
return;
p->size=1;
if(p->l)
p->size+=p->l->size;
if(p->r)
p->size+=p->r->size;
}
node *New(int x)
{
node *p=new node;
p->val=x;
p->rnd=rand();
p->size=1;
p->l=p->r=NULL;
return p;
}
node *merge(node *x,node *y)
{
if(x==NULL)
return y;
if(y==NULL)
return x;
if(x->rnd<=y->rnd)
{
x->r=merge(x->r,y);
update(x);
return x;
}
else
{
y->l=merge(x,y->l);
update(y);
return y;
}
}
void split(node *p,int val,node *&x,node *&y)
{
if(p==NULL)
x=y=NULL;
else
{
if(p->val<=val)
{
x=p;
split(p->r,val,p->r,y);
}
else
{
y=p;
split(p->l,val,x,p->l);
}
update(p);
}
}
void insert(int x)
{
node *a=NULL,*b=NULL;
split(root,x,a,b);
root=merge(merge(a,New(x)),b);
}
int Rank(int x)
{
node *a=NULL,*b=NULL;
split(root,x-1,a,b);
int ans=a->size+1;
merge(a,b);
return ans;
}
int num(int x)
{
node *p=root;
while(p!=NULL)
{
if(p->l->size+1==x)
break;
else if(p->l->size+1<x)
x-=p->l->size+1,p=p->r;
else
p=p->l;
}
return p->val;
}
void del(int x)
{
node *a=NULL,*b=NULL,*c=NULL;
split(root,x,b,c);
split(b,x-1,a,b);
root=merge(merge(a,merge(b->l,b->r)),c);
}
int pre(int x)
{
int ans;
node *a=NULL,*b=NULL;
split(root,x-1,a,b);
node *p=a;
while(p!=NULL)
ans=p->val,p=p->r;
merge(a,b);
return ans;
}
int nxt(int x)
{
int ans;
node *a=NULL,*b=NULL;
split(root,x,a,b);
node *p=b;
while(p!=NULL)
ans=p->val,p=p->l;
merge(a,b);
return ans;
}
int n;
int main()
{
cin>>n;
while(n--)
{
int op,x;
cin>>op>>x;
if(op==1)
insert(x);
else if(op==2)
del(x);
else if(op==3)
cout<<Rank(x)<<endl;
else if(op==4)
cout<<num(x)<<endl;
else if(op==5)
cout<<pre(x)<<endl;
else
cout<<nxt(x)<<endl;
}
}