#5测试点数据在本地测是全对的, 评测时候就wa了
#5测试点数据
50
1 577793
1 408221
1 880861
2 408221
1 460353
1 223489
6 577713
4 2
5 889905
2 880861
1 100033
1 73956
1 22575
5 583761
6 571549
1 812645
4 3
1 643621
1 451623
6 14895
1 556691
4 1
1 225789
2 22575
1 632329
3 73956
1 316785
5 101413
4 11
5 639414
6 636353
1 272382
1 434049
2 643621
1 99617
2 577793
1 921581
1 894033
3 223489
1 767367
3 272382
1 642721
1 272033
3 632329
1 737721
1 864513
5 746457
1 877545
1 51097
1 484817
#include <bits/stdc++.h>
//#define int long long
#define re register
using namespace std;
const int N=1e6+1e5+10;
int n,m,a[N],last=0,cnt=0,root=0;
struct node {
int ls=0,rs=0,siz=0,key=0,pri=0;
}t[N];
void update(int x){
t[x].siz=t[t[x].ls].siz+t[t[x].rs].siz+1;
}
void newnode(int x){
t[++cnt].siz=1;
t[cnt].ls=t[cnt].rs=0;
t[cnt].key=x;
t[cnt].pri=rand();
}
void rotate(int &o,int d){
int k;
if (d==1){//左旋
k=t[o].rs;
t[o].rs=t[k].ls;
t[k].ls=o;
}
else {//右旋
k=t[o].ls;
t[o].ls=t[k].rs;
t[k].rs=o;
}
t[k].siz=t[o].siz;
update(o);
o=k;
}
void insert(int &u,int x){
if (u==0){
newnode(x);
u=cnt;
return ;
}
t[u].siz++;
if (x>=t[u].key)insert(t[u].rs,x);
else insert(t[u].ls,x);
if (t[u].ls&&t[u].pri<t[t[u].ls].pri)rotate(u,0);
if (t[u].rs&&t[u].pri<t[t[u].rs].pri)rotate(u,1);
update(u);
}
void del(int &u,int x){
t[u].siz--;
if (t[u].key==x){
if (t[u].ls==0&&t[u].rs==0){
u=0;
return ;
}
if (t[u].ls==0||t[u].rs==0){
u=t[u].ls+t[u].rs;
return ;
}
if (t[t[u].ls].pri<t[t[u].rs].pri){
rotate(u,1);
return;
}
else {
rotate(u,0);
return;
}
}
if (t[u].key>=x)del(t[u].ls,x);
else del(t[u].rs,x);
update(u);
}
int Rank(int u,int x){
if (u==0)return 0;
if (x>t[u].key)return t[t[u].ls].siz+1+Rank(t[u].rs,x);
return Rank(t[u].ls,x);
}
int kth(int u,int k){
if (k==t[t[u].ls].siz+1)return t[u].key;
if (k>t[t[u].ls].siz+1)return kth(t[u].rs,k-(t[t[u].ls].siz+1));
else return kth(t[u].ls,k);
}
int pre(int u,int x){
if (u==0)return 0;
if (t[u].key>=x)return pre(t[u].ls,x);
int tmp=pre(t[u].rs,x);
if (tmp==0)return t[u].key;
return tmp;
}
int suc(int u,int x){
if (u==0)return 0;
if (t[u].key<=x)return suc(t[u].rs,x);
int tmp=suc(t[u].ls,x);
if (tmp==0)return t[u].key;
return tmp;
}
signed main() {
//freopen("P3369_5.in","r",stdin);
scanf("%d",&n);
for (int i=1,op,x;i<=n;i++){
scanf("%d%d",&op,&x);
//x^=last;
if (op==1)insert(root,x);
else if (op==2)del(root,x);
else if (op==3)printf("%d\n",Rank(root,x)+1);
else if (op==4)printf("%d\n",kth(root,x));
else if (op==5)printf("%d\n",pre(root,x));
else printf("%d\n",suc(root,x));
}
//cout<<kth(root,14)<<" ";
return 0;
}