#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;++i)
#define per(i,a,b) for(int i=a;i>=b;--i)
#define max(a,b) ((a)>(b)?a:b)
#define wr(x) write(x,false),pc('\n');
#define pc putchar
#define gc(ch) while(ch=getchar(),ch==' '||ch=='\n')
#define int long long
using namespace std;
inline void write(int ans, bool bk) { if (ans < 0)putchar('-'), ans = -ans; if (ans == 0) { if (!bk)putchar('0'); return; }write(ans / 10, true); putchar(ans % 10 ^ '0'); }
inline bool qr(int& ret) { int x = 0, f = 0; char ch = getchar(); while (ch < '0' || ch>'9'){f |= ch == '-', ch = getchar();if(f)break;}while (ch >= '0' && ch <= '9')x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar(); ret = f ? -x : x; return f&&x==0;}
struct Treap{
int val,cnt,priority,size;
Treap *lc,*rc;
}*Root,*null;
Treap* creat(int val){Treap *p=new Treap;p->cnt=1,p->priority=rand(),p->val=val,p->lc=p->rc=null,p->size=1;return p;}
pair<Treap*,Treap*> split(Treap *p,int key){
if(p==null)return make_pair(null,null);
if(p->val<=key){
p->size-=p->rc->size;
pair<Treap*,Treap*> o=split(p->rc,key);
p->rc=o.first;
p->size+=p->rc->size;
return make_pair(p,o.second);
}
else{
p->size-=p->lc->size;
pair<Treap*,Treap*> o=split(p->lc,key);
p->lc=o.second;
p->size+=p->lc->size;
return make_pair(o.first,p);
}
}
Treap* merge(Treap *p,Treap *q){
if(p==null)return q;
if(q==null)return p;
if(p->priority>q->priority){
p->size+=q->size;
p->rc=merge(p->rc,q);
return p;
}
else{
q->size+=p->size;
q->lc=merge(p,q->lc);
return q;
}
}
Treap* find(Treap *p,int key){
if(p==null)return null;
if(p->val==key)return p;
else if(p->val>key)return find(p->lc,key);
else return find(p->rc,key);
}
void insert(int key){
if(Root==null){
Root=creat(key);
return;
}
Treap *p=find(Root,key);
if(p!=null)++p->cnt,++p->size;
else{
pair<Treap*,Treap*> o=split(Root,key);
o.first=merge(o.first,creat(key));
Root=merge(o.first,o.second);
}
}
void erase(int key){
Treap *p=find(Root,key);
if(p==null)return;
else if(p->cnt>1)--p->cnt,--p->size;
else{
pair<Treap*,Treap*> o=split(Root,key);
pair<Treap*,Treap*> l=split(o.first,key-1);
delete l.second;
Root=merge(l.first,o.second);
}
}
int findmax(Treap *p){
if(p->rc==null)return p->val;
return findmax(p->rc);
}
int findmin(Treap *p){
if(p->lc==null)return p->val;
return findmin(p->lc);
}
int queryrank(Treap* p,int key){
if(key==p->val)return p->lc->size+1;
if(key<p->val)return queryrank(p->lc,key);
if(key>p->val)return queryrank(p->rc,key)+p->lc->size+p->cnt;
}
int findrank(Treap* p,int rnk){
if(rnk-p->lc->size<=p->cnt&&rnk-p->lc->size>0)return p->val;
if(rnk<=p->lc->size+p->cnt)return findrank(p->lc,rnk);
return findrank(p->rc,rnk-p->lc->size-p->cnt);
}
int upper(int key){
pair<Treap*,Treap*> o=split(Root,key-1);
int res=findmax(o.first);
Root=merge(o.first,o.second);
return res;
}
int lower(int key){
pair<Treap*,Treap*> o=split(Root,key);
int res=findmin(o.second);
Root=merge(o.first,o.second);
return res;
}
signed main(){
null=new Treap;
null->cnt=null->priority=null->size=null->val=0;
Root=null;
int n;
qr(n);
while(n--){
int opt,x;
qr(opt),qr(x);
if(opt==1)insert(x);
if(opt==2)erase(x);
if(opt==3)wr(queryrank(Root,x));
if(opt==4)wr(findrank(Root,x));
if(opt==5)wr(upper(x));
if(opt==6)wr(lower(x));
}
}