#include <bits/stdc++.h>
using namespace std;
int root=1;
int sum=root;
struct splay_tree
{
int father;
int children[2]={-1};
int size=1;
int val;
}tree[1000005];
void new_size(int x)
{
tree[x].size=tree[tree[x].children[0]].size+tree[tree[x].children[0]].size;
}
void splay(int x,int goal)
{
if (tree[x].father==goal)
{
new_size(x);
return ;
}
if (tree[x].children[0]!=root&&tree[x].children[1]!=root)
{
if (x<tree[x].father)
{
tree[tree[x].father].children[0]=tree[x].children[0];
tree[tree[x].children[0]].father=tree[x].father;
tree[x].father=tree[tree[x].father].father;
tree[tree[x].father].father=x;
tree[tree[x].father].children[0]=x;
}
else
{
tree[tree[x].father].children[1]=tree[x].children[1];
tree[tree[x].children[1]].father=tree[x].father;
tree[x].father=tree[tree[x].father].father;
tree[tree[x].father].father=x;
tree[tree[x].father].children[1]=x;
}
}
else
{
if (tree[goal].val>tree[root].val)
{
tree[x].father=tree[x].children[1];
tree[x].children[1]=tree[tree[x].children[1]].children[1];
tree[tree[x].father].children[0]=x;
}
else
{
tree[x].father=tree[x].children[0];
tree[x].children[1]=tree[tree[x].children[0]].children[0];
tree[tree[x].father].children[1]=x;
}
}
splay(x,goal);
}
void rootate(int x)
{
if (tree[root].val<tree[x].val)
{
splay(root,tree[x].children[0]);
tree[x].children[0]=root;
}
else
{
splay(root,tree[x].children[1]);
tree[x].children[1]=root;
}
tree[root].father=x;
root=x;
new_size(x);
sum=root;
}
void insert(int x,int place)
{
if (tree[x].val<=x)
{
if (tree[place].children[0]!=-1)
{
insert(x,tree[place].children[0]);
}
else
{
tree[x].children[0]=x;
new_size(x);
return ;
}
}
else
{
if (tree[place].children[1]!=-1)
{
insert(x,tree[place].children[1]);
}
else
{
tree[x].children[1]=x;
new_size(x);
return ;
}
}
}
int find_last(int x,int place)
{
if (tree[place].val<x)
{
return place;
}
else
{
find_last(x,tree[tree[x].children[0]].children[1]);
}
}
int find_next(int x,int place)
{
if (tree[place].val>x)
{
return place;
}
else
{
find_next(x,tree[tree[x].children[1]].children[0]);
}
}
void delete_num(int x)
{
int last=find_last(x,root);
int next=find_next(x,root);
splay(x,next);
}
int find_kth(int x,int place)
{
if (sum==x)
{
sum=root;
return tree[x].val;
}
if (sum>x)
{
sum++;
find_kth(x,tree[place].children[0]);
}
else
{
sum--;
find_kth(x,tree[place].children[0]);
}
}
int find_k(int x)
{
return find_last(x,root)+1;
}
int main()
{
int n;
cin>>n;
int id=1;
for (int i=1;i<=n;i++)
{
int opt,x;
cin>>opt>>x;
if (opt==1)
{
tree[id].val=x;
id++;
insert(x,root);
}
if (opt==2)
{
delete_num(x);
}
if (opt==3)
{
cout<<find_k(x)<<"\n";
}
if (opt==4)
{
cout<<find_kth(x,root)<<"\n";
}
if (opt==5)
{
cout<<find_last(x,root)<<"\n";
}
if (opt==6)
{
cout<<find_next(x,root)<<"\n";
}
}
return 0;
}