#include<bits/stdc++.h>
using namespace std;
struct A{
A* l;
A* r;
int val;
int fix;
int weight;
int sum;
};
A* root;
int n,T,x;
int tag,nodesum;
A* zxuan(A*root)
{
if(root==NULL) return NULL;
A* q=root->r;
root->r=q->l;
q->l=root;
return q;
}
A* yxuan(A* root)
{
if(root==NULL) return NULL;
A* q=root->l;
root->l=q->r;
q->r=root;
return q;
}
void up(A* &root)
{
root->sum=root->weight;
if(root->l) root->sum+=root->l->sum;
if(root->r) root->sum+=root->r->sum;
}
A* cha(A* root,int val)
{
if(root==NULL)
{
A* q=new A();
q->fix=rand();
q->val=val;
q->weight=1;
up(q);
return q;
}
if(val<root->val)
{
root->l=cha(root->l,val);
if(root->l->val<root->val)
{
root=yxuan(root);
}
up(root);
return root;
}
else if(val>root->val)
{
root->r=cha(root->r,val);
if(root->l)
{
if(root->l->val<root->val)
{
root=zxuan(root);
}
up(root);
return root;
}
else
{
return root;
}
}
else root->weight++;up(root);
return root;
}
A* shan(A* root,int val)
{
if(root==NULL) return NULL;
if(val<root->val)
{
up(root);
return shan(root->l,val);
}
else if(val>root->val)
{
up(root);
return shan(root->r,val);
}
if(root->l==NULL && root->r==NULL)
{
if(root->weight--)
{
up(root);
return root;
}
else
{
delete root;
return NULL;
}
}
if(root->l!=NULL && root->r!=NULL)
{
if(root->l->fix<root->r->fix)
{
root=yxuan(root);
up(root);
return shan(root->r,val);
}
else
{
root=zxuan(root);
up(root);
return shan(root->l,val);
}
}
if(root->l!=NULL)
{
if(root->weight--)
{
up(root);
return root;
}
else
{
A* q=root->l;
delete root;
return q;
}
}
else
{
if(root->weight--)
{
up(root);
return root;
}
else
{
A* q=root->r;
delete root;
return q;
}
}
}
int dian_lev(A* root,int val)
{
if(root==NULL)
{
return 0;
}
if(val>root->val)
{
return root->l->sum+1+dian_lev(root->r,val);
}
else
{
return dian_lev(root->l,val);
}
}
int lev_dian(A* root,int lev)
{
if(root==NULL)
{
return 0;
}
if(root->l)
{
if(lev==root->l->sum+1)
{
return root->val;
}
if(lev>root->l->sum+1)
{
return lev_dian(root->r,lev-root->l->sum-1);
}
else
{
return lev_dian(root->l,lev);
}
}
else
{
if(lev==root->sum)
{
return root->val;
}
else
{
return lev_dian(root->r,lev);
}
}
}
void zhaoqq(A* root,int val)
{
if(root==NULL)
{
return;
}
if(val<=root->val)
{
tag=root->val;
zhaoqq(root->r,val);
}
else
{
zhaoqq(root->l,val);
}
}
void zhaohj(A* root,int val)
{
if(root==NULL)
{
return;
}
if(val>=root->val)
{
tag=root->val;
zhaohj(root->l,val);
}
else
{
zhaohj(root->r,val);
}
}
void sc(A* root)
{
if(root==NULL) return;
for(int i=1;i<=nodesum;i++)
{
cout<<lev_dian(root,i)<<" ";
}
cout<<"\n";
}
int main()
{
cin>>n;
while(n--)
{
tag=0;
scanf("%d%d",&T,&x);
if(T==1)
{
nodesum++;
root=cha(root,x);
}
if(T==2)
{
nodesum--;
root=shan(root,x);
}
if(T==3)
{
int num=dian_lev(root,x);
printf("%d\n",num);
}
if(T==4)
{
int num=lev_dian(root,x);
printf("%d\n",num);
}
if(T==5)
{
zhaoqq(root,x);
printf("%d\n",tag);
}
if(T==6)
{
zhaohj(root,x);
printf("%d\n",tag);
}
sc(root);
}
}