#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<ctime>
#include<algorithm>
using namespace std;
#define INF 1000000000
#define FILE "read"
int n,len,root,top,stack[100005];
namespace INIT{
char buf[1<<15],*fs,*ft;
inline char getc() {return (fs==ft&&(ft=(fs=buf)+fread(buf,1,1<<15,stdin),fs==ft))?0:*fs++;}
inline int read(){
int x=0,f=1; char ch=getc();
while(!isdigit(ch)) {if(ch=='-') f=-1; ch=getc();}
while(isdigit(ch)) {x=x*10+ch-'0'; ch=getc();}
return x*f;
}
}using namespace INIT;
namespace TiZuiYang_Tree{
const double chty=0.75;
struct node{int son[2],f,size,v;}tr[100005];
void init(){
len=2; root=1;
tr[1].size=2; tr[1].v=-INF; tr[1].son[1]=2;
tr[2].size=1; tr[2].v=INF; tr[2].f=1;
}
bool balance(int x) {
double p=tr[x].size*chty;
return p>=(double)tr[tr[x].son[0]].size&&p>=(double)tr[tr[x].son[1]].size;
}
void dfs(int x){
if(!x) return;
dfs(tr[x].son[0]);
stack[++top]=x;
dfs(tr[x].son[1]);
}
int build(int l,int r){
if(l>r) return 0;
int mid=(l+r)/2,x=stack[mid];
tr[tr[x].son[0]=build(l,mid-1)].f=x;
tr[tr[x].son[1]=build(mid+1,r)].f=x;
tr[x].size=tr[tr[x].son[0]].size+tr[tr[x].son[1]].size+1;
return x;
}
void rebuild(int x){
top=0; dfs(x);
int fa=tr[x].f,which=(tr[fa].son[1]==x);
int sonroot=build(1,top);
tr[tr[fa].son[which]=sonroot].f=fa;
if(x==root) root=sonroot;
}
int find(int v){
int now=root;
while(now){
if(v==tr[now].v) return now;
else now=tr[now].son[v>tr[now].v];
}
}
void insert(int v){
int now=root,p=++len;
tr[p].v=v; tr[p].size=1;
while(1){
tr[now].size++;
int which=(v>=tr[now].v);
if(tr[now].son[which]) now=tr[now].son[which];
else {tr[tr[now].son[which]=p].f=now; break;}
}
int id=0;
for(int i=p;i;i=tr[i].f) if(!balance(i)) id=i;
if(id) rebuild(id);
}
void del(int x){
if(tr[x].son[0]&&tr[x].son[1]){
int p=tr[x].son[0];
while(tr[p].son[1]) p=tr[p].son[1];
tr[x].v=tr[p].v; x=p;
}
int Son=(tr[x].son[0])?tr[x].son[0]:tr[x].son[1],which=(tr[tr[x].f].son[1]==x);
tr[tr[tr[x].f].son[which]=Son].f=tr[x].f;
for(int i=tr[x].f;i;i=tr[i].f) tr[i].size--;
if(x==root) root=Son;
}
int rank(int v){
int now=root,ans=0;
while(now){
if(tr[now].v<v) {ans+=tr[tr[now].son[0]].size+1; now=tr[now].son[1];}
else now=tr[now].son[0];
}
return ans;
}
int kth(int x){
int now=root;
while(1){
if(tr[tr[now].son[0]].size==x-1) return now;
else if(tr[tr[now].son[0]].size>=x) now=tr[now].son[0];
else x-=tr[tr[now].son[0]].size+1,now=tr[now].son[1];
}
return now;
}
int before(int v){
int now=root,ans=-INF;
while(now){
if(tr[now].v<v) ans=max(ans,tr[now].v),now=tr[now].son[1];
else now=tr[now].son[0];
}
return ans;
}
int after(int v){
int now=root,ans=INF;
while(now){
if(tr[now].v>v) ans=min(ans,tr[now].v),now=tr[now].son[0];
else now=tr[now].son[1];
}
return ans;
}
}using namespace TiZuiYang_Tree;
int main(){
freopen(FILE".in","r",stdin);
freopen(FILE".out","w",stdout);
init();
n=read();
for(int i=1;i<=n;i++){
int flag=read(),x=read();
if(flag==1) insert(x);
if(flag==2) del(find(x));
if(flag==3) printf("%d\n",rank(x));
if(flag==4) printf("%d\n",tr[kth(x+1)].v);
if(flag==5) printf("%d\n",before(x));
if(flag==6) printf("%d\n",after(x));
}
return 0;
}