#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=4e6+10;
struct node{
int data;
int lson,rson;
int sz;
int fa;
int lnum,rnum;
int num;
}tree[N];
int tot,n,root;
struct Splay{
void clear(int x){
tree[x]={0,0,0,0,0,0,0,0};
return;
}
void zag(int x){
int father=tree[x].fa;
tree[father].rson=tree[x].lson;
if(tree[x].lson){
tree[tree[x].lson].fa=father;
}
tree[x].fa=tree[father].fa;
if(tree[father].fa){
if(father==tree[tree[father].fa].lson){
tree[tree[father].fa].lson=x;
}else{
tree[tree[father].fa].rson=x;
}
}
tree[x].lson=father;
tree[father].fa=x;
tree[father].rnum=tree[x].lnum;
tree[x].lnum=tree[father].rnum+tree[father].lnum+1;
return;
}
void zig(int x){
int father=tree[x].fa;
tree[father].lson=tree[x].rson;
if(tree[x].rson){
tree[tree[x].rson].fa=father;
}
tree[x].fa=tree[father].fa;
if(tree[father].fa){
if(father==tree[tree[father].fa].lson){
tree[tree[father].fa].lson=x;
}else{
tree[tree[father].fa].rson=x;
}
}
tree[x].rson=father;
tree[father].fa=x;
tree[father].lnum=tree[x].rnum;
tree[x].rnum=tree[father].rnum+tree[father].lnum+1;
return;
}
void splay(int x){
int father;
while(tree[x].fa){
father=tree[x].fa;
if(tree[father].fa==0){
if(x==tree[father].lson){
zig(x);
}else{
zag(x);
}
break;
}
if(x==tree[father].lson){
if(father==tree[tree[father].fa].lson){
zig(father);
zig(x);
}else{
zig(x);
zag(x);
}
}else{
if(father==tree[tree[father].fa].rson){
zag(father);
zag(x);
}else{
zag(x);
zig(x);
}
}
}
root=x;
return;
}
void insert(int x){
int place,father;
father=root;
while(father!=0){
place=father;
if(x<=tree[father].data){
tree[father].lnum++;
father=tree[father].lson;
}else{
tree[father].rnum++;
father=tree[father].rson;
}
}
tot++;
tree[tot].data=x;
tree[tot].lson=0;
tree[tot].rson=0;
tree[tot].lnum=0;
tree[tot].rnum=0;
tree[tot].fa=0;
if(root==0){
root=tot;
return;
}
tree[tot].fa=place;
if(x<=tree[place].data){
tree[place].lson=tot;
}else{
tree[place].rson=tot;
}
splay(tot);
return;
}
int find(int x){
int father=root;
while(father){
if(tree[father].data==x){
splay(father);
return 1;
}
if(tree[father].data<x){
father=tree[father].lson;
}else{
father=tree[father].rson;
}
}
return 0;
}
int rk(int x){
int ans=0,father=root;
while(1){
if(x<tree[father].data){
father=tree[father].lson;
}else{
ans+=tree[father].lnum;
if(father==0){
return ans+1;
}
if(x==tree[father].data){
splay(father);
return ans+1;
}
father=tree[father].rson;
}
}
return 0;
}
int Find(int k){
int father=root;
if(tree[father].lnum+tree[father].rnum+1<k){
return -1;
}
while(1){
if(tree[father].rnum+1==k){
return tree[father].data;
}
if(tree[father].rnum>=k){
father=tree[father].rson;
}else{
k=k-tree[father].rnum-1;
father=tree[father].lson;
}
}
return 0;
}
int before(int x){
find(x);
int child=tree[root].lson;
while(child){
if(tree[child].rson==0) break;
child=tree[child].rson;
}
if(child){
return tree[child].data;
}
return 0;
}
int nxt(int x){
find(x);
int child=tree[root].rson;
while(child){
if(tree[child].lson==0) break;
child=tree[child].lson;
}
if(child){
return tree[child].data;
}
return 0;
}
void del(int x){
find(x);
int father=root;
int ls=tree[father].lson,rs=tree[father].rson;
if(ls==0&&rs==0){
root=0;
return;
}
if(ls==0){
root=rs;
tree[rs].fa=0;
return;
}
if(rs==0){
root=ls;
tree[ls].fa=0;
return;
}
father=ls;
tree[ls].fa=0;
while(tree[father].rson){
father=tree[father].rson;
}
splay(father);
tree[father].rson=rs;
tree[rs].fa=father;
tree[father].rnum=tree[rs].lnum+tree[rs].rnum+1;
return;
}
}spl;
signed main(){
cin>>n;
while(n--){
int opt,x;
cin>>opt>>x;
if(opt==1){
spl.insert(x);
}else if(opt==2){
spl.del(x);
}else if(opt==3){
int optt=spl.find(x);
if(optt==0){
spl.insert(x);
}
cout<<" ";
cout<<spl.rk(x)<<endl;
if(optt==0){
spl.del(x);
}
}else if(opt==4){
cout<<" ";
cout<<spl.Find(x)<<endl;
}else if(opt==5){
int optt=spl.find(x);
if(optt==0){
spl.insert(x);
}
cout<<" ";
cout<<spl.before(x)<<endl;
if(optt==0){
spl.del(x);
}
}else{
int optt=spl.find(x);
if(optt==0){
spl.insert(x);
}
cout<<" ";
cout<<spl.nxt(x)<<endl;
if(optt==0){
spl.del(x);
}
}
}
return 0;
}