#include<bits/stdc++.h>
using namespace std;
#define lovelive long long
int tot;
int pos[100020];
int p[100020];
map <int,int> s;
priority_queue<int,vector<int>,greater<int> > so;
struct question
{
int num,x;
}q[100020];
struct tree
{
int l,r,sum;
}t[400020];
void buildtree(int i,int l,int r)
{
t[i].l=l;
t[i].r=r;
if(l==r)
{
pos[l]=i;
return ;
}
int mid=(l+r)>>1;
buildtree(i<<1,l,mid);
buildtree(i<<1|1,mid+1,r);
}
void insert(int i,int point)
{
t[i].sum++;
int mid=(t[i].l+t[i].r)>>1;
if(t[i].l==t[i].r)
return ;
if(point>mid)
insert(i<<1|1,point);
else
insert(i<<1,point);
}
void Delete(int i,int point)
{
t[i].sum--;
int mid=(t[i].l+t[i].r)>>1;
if(t[i].l==t[i].r)
return ;
if(point>mid)
Delete(i<<1|1,point);
else
Delete(i<<1,point);
}
int find_num(int i,int x)
{
if(t[i].l==t[i].r)
return t[i].l;
if(x>t[i<<1].sum)
find_num(i<<1|1,x-t[i<<1].sum);
else
find_num(i<<1,x);
}
int query(int i,int l,int r)
{
if(r<t[i].l||l>t[i].r)
return 0;
if(r>=t[i].r&&l<=t[i].l)
return t[i].sum;
return query(i<<1,l,r)+query(i<<1|1,l,r);
}
int find_rank(int x)
{
return query(1,1,x-1)+1;
}
int find_pre(int x)
{
int p=find_rank(x);
return find_num(1,p-1);
}
int find_next(int x)
{
int p=find_rank(x);
return find_num(1,p+t[pos[x]].sum);
}
int main()
{
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d%d",&q[i].num,&q[i].x);
if(q[i].num!=4)
so.push(q[i].x);
}
int point = -2e9-1;
while(!so.empty())
{
if(so.top()!=point)
++tot;
s[so.top()]=tot;
p[tot]=so.top();
point=so.top();
so.pop();
}
buildtree(1,1,tot);
for(int i=1;i<=n;i++)
if(q[i].num!=4)
q[i].x=s[q[i].x];
for(int i=1;i<=n;i++)
{
switch(q[i].num)
{
case 1:insert(1,q[i].x);break;
case 2:Delete(1,q[i].x);break;
case 4:cout<<find_rank(q[i].x)<<"\n";break;
case 3:cout<<p[find_num(1,q[i].x)]<<"\n";break;
case 5:cout<<p[find_pre(q[i].x)]<<"\n";break;
case 6:cout<<p[find_next(q[i].x)]<<"\n";break;
}
}
return 0;
}