#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<vector>
using namespace std;
#include<stdlib.h>
struct tree{
int key;//域值
int l; int lm;//左编号 左长
int r; int rm;
int sum;
int f;//编号
int times;
}a[999999];
int root=0,tot=0;
void turnleft(int now)
{
if(a[now].f ==root)
root=now;
int ff= a[a[now].f ].f ;
int fa=a[now].f;
a[a[now].f ].f =now;
a[now].f=ff;
if(ff!=0)
{
if(a[ff].l ==a[now].f )
a[ff].l=now;
if(a[ff].r==a[now].f)
a[ff].r=now;
}
if(a[now].l!=0)
a[a[now].l].f=fa;
a[fa].r=a[now].l;
a[now].l=fa;
//以下是子树大小
a[fa].rm=a[now].lm;
a[now].lm+=a[fa].lm+1 ;
}
void turnright(int now)
{
if(a[now].f==root)
root=now;
int fa=a[now].f;
int ff=a[fa].f;
a[fa].f=now;
a[now].f=ff;
if(ff!=0)
{
if(a[ff].l==fa)
a[ff].l=now;
if(a[ff].r ==fa)
a[ff].r=now;
}
if(a[now].r!=0)
a[a[now].r].f=fa;
a[fa].l=a[now].r;
a[now].r=fa;
a[fa].lm=a[now].rm;
a[now].rm+=a[fa].rm+1;
}
void add(int x)
{
++tot;
a[tot].key =x;
a[tot].sum=rand()%1000050;
a[tot].times=1;
if(tot==1)
{
root=tot;
return ;
}
int pre=root,now=root;
while(now)
{
if(x==a[now].key )
{
a[now].times++;
return;//有重复元素
}
if(x<a[now].key )
{
a[now].lm++;
pre=now;
now=a[now].l;
continue;
}
if(x>a[now].key )
{
a[now].rm++;
pre=now;
now=a[now].r;
continue;
}
}
if(x<a[pre].key )
a[pre].l=now;
if(x>a[pre].key )
a[pre].r=now;
a[now].f=pre; now=tot;
while(a[now].sum<a[a[now].f].sum)
{
if(a[a[now].f ].l==now)
{
turnright(now);
continue;
}
if(a[a[now].f].r==now)
{
turnleft(now);
continue;
}
}
}
/*
void add(int x,int now)
{
if(a[now].lm ==0&&x<a[now].key )
{
a[++tot].key=x;
a[now].l=tot;
a[tot].f=now;
a[now].lm++;
return ;
}
if(a[now].rm ==0&&x>=a[now].key )
{
a[++tot].key=x;
a[now].r=tot;
a[tot].f=now;
a[now].rm++;
return ;
}
//if(x==a[now].key) return ;
if(x<a[now].key) add(x,a[now].l),a[now].lm++;else
if(x>=a[now].key) add(x,a[now].r),a[now].rm++;
return ;
}
*/
int findw(int x,int now)
{
if(a[now].key==x) return now;
if(x<a[now].key) return findw(x,a[now].l);
if(x>a[now].key) return findw(x,a[now].r);
}
void balance()
{
while(a[root].lm >a[root].rm+tot/4)
{
a[root].f=a[root].l;
a[a[root].l ].r=root;
root=a[root].l;
}
while(a[root].lm <=a[root].rm+tot/4)
{
a[root].f=a[root].r;
a[a[root].r ].l=root;
root=a[root].r;
}
return ;
}
/*
void delet(int x)
{
int now=findw(x,root);
if(now==root)
{
if(a[now].lm >a[now].rm )
{
a[a[root].l].r=a[root].r;
root=a[root].l;
}
}
}
*/
void delet(int x)
{
int now=findw(x,root);
while(!a[now].l||!a[now].r)
{
if(a[now].l&&a[now].r==0)
{
turnright(a[now].l);
continue;
}
if(a[now].r==0&&a[now].r)
{
turnleft(a[now].r);
continue;
}
if(a[now].l&&a[now].r)
{
if(a[a[now].l].sum<a[a[now].r].sum)
turnright(a[now].l);
else turnleft(a[now].r);
}
if(a[now].f)
{
if(a[a[now].f].l==now)
a[a[now].f].l=0;
if(a[a[now].f].r==now)
a[a[now].f].r=0;
}
}
return ;
}
int find3(int x,int now)
{
if(a[now].lm ==x-1) return a[now].key ;
if(a[now].lm >x-1) return find3(x,a[now].l );
if(a[now].lm<x-1) return find3(x-a[now].lm-1,a[now].r );
}
int find4(int x,int now)
{
if(a[now].key==x) return a[now].lm+1;
if(x<a[now].key) return find4(x,a[now].l);
if(x>a[now].key) return find4(x,a[now].r)+a[now].lm+1;
}
int find5(int x,int now)
{
int w=a[now].key ;
if(w==x) return w;
if(w>x)
{
if(a[now].lm ==0) return w;
return find5(x,a[now].l );
}
if(w<x)
{
if(a[now].rm ==0) return a[a[now].f ].key;
return find5(x,a[now].r );
}
}
int find6(int x)
{
int now=findw(x,root);
if(a[now].lm!=0)
return a[a[now].l].key;
while(a[now].key >x)
{
now=a[now].f ;
}
return a[now].key ;
}
int find7(int x)
{
int now=findw(x,root);
if(a[now].rm!=0)
return a[a[now].r].key;
while(a[now].key <x)
{
now=a[now].f ;
}
return a[now].key ;
}
int main()
{
int p,x,n;
int pp[20],z[20];
root=0,tot=0;
cin>>n;
for(int i=1;i<=n;i++)
scanf("%d%d",&pp[i],&z[i]);
for(int i=1;i<=n;i++)
{
p=pp[i];x=z[i];
if(p==1) add(x);
if(p==3) printf("%d\n",find3(x,root));//找第K小值
if(p==4) printf("%d\n",find4(x,root));
if(p==5) printf("%d\n",find5(x,root));
if(p==6) printf("%d\n",find6(x));
if(p==7) printf("%d\n",find7(x));
}
return 0;
}