#include<bits/stdc++.h>
#define MAXN 100005
#define INF 0x3f3f3f3f
using namespace std;
typedef struct TreapNode {
int cnt;
int val;
int pri;
TreapNode* lchild;
TreapNode* rchild;
TreapNode(int val=0) {
lchild=rchild=NULL;
cnt=1;
this->val=val;
pri=rand();
}
} TreapNode,*Tree;
int RANK[MAXN];
void left_rotate(Tree &node) {
Tree temp=node->rchild;
node->rchild=temp->lchild;
temp->lchild=node;
node=temp;
}
void right_rotate(Tree &node) {
Tree temp=node->lchild;
node->lchild=temp->rchild;
temp->rchild=node;
node=temp;
}
void insert(Tree &root,int val) {
if(root==NULL) {
root =new TreapNode(val);
return;
} else if(val==root->val) {
root->cnt++;
return;
} else if(val>root->val) {
insert(root->rchild,val);
if(root->rchild->pri<root->pri)
left_rotate(root);
return;
} else if(val<root->val) {
insert(root->lchild,val);
if(root->lchild->pri<root->pri)
right_rotate(root);
return;
}
}
void remove(Tree &root,int val) {
if(val==root->val) {
if(root->cnt>1) {
root->cnt--;
return;
} else {
Tree *node=&root;
while((*node)->lchild&&(*node)->rchild) {
if((*node)->lchild->pri<(*node)->rchild->pri) {
right_rotate((*node));
node=&((*node)->rchild);
} else {
left_rotate((*node));
node=&((*node)->lchild);
}
}
if((*node)->lchild==NULL) {
(*node)=(*node)->rchild;
} else if((*node)->rchild==NULL) {
(*node)=(*node)->lchild;
}
return;
}
} else if(val>root->val) {
remove(root->rchild,val);
return;
} else if(val<root->val) {
remove(root->lchild,val);
return;
}
}
void in_fr(Tree root,int val,int &rank,bool &ok) {
if(ok) return;
if(!root) return;
else {
if(!ok) in_fr(root->lchild,val,rank,ok);
if(!ok&&root->val==val) {
ok=true;
return;
}
if(!ok) rank++;
if(!ok) in_fr(root->rchild,val,rank,ok);
}
}
void in_fx(Tree root,int rank,int &now_rank,bool &ok) {
if(ok) return;
if(!root) return;
else {
if(!ok) in_fx(root->lchild,rank,now_rank,ok);
if(!ok&&now_rank==rank) {
cout<<root->val<<endl;
ok=true;
return;
}
if(!ok) now_rank++;
if(!ok) in_fx(root->rchild,rank,now_rank,ok);
}
}
void find_front(Tree root,int val,int &e,bool &ok,int &rank) {
if(ok) return;
if(!root) return;
else {
if(!ok) find_front(root->lchild,val,e,ok,rank);
if(!ok&&val<=root->val) {
e=RANK[rank-1];
ok=true;
return;
}
if(!ok) RANK[rank++]=root->val;
if(!ok) find_front(root->rchild,val,e,ok,rank);
}
}
void find_back(Tree root,int val,int &e,bool &ok) {
if(ok) return;
if(!root) return;
else {
if(!ok) find_back(root->lchild,val,e,ok);
if(!ok&&root->val>val) {
e=root->val;
ok=true;
return;
}
if(!ok) find_back(root->rchild,val,e,ok);
}
}
void in(Tree root) {
if(!root) return;
else {
in(root->lchild);
cout<<root->val<<' '<<root->pri<<' '<<root->cnt<<endl;
in(root->rchild);
}
}
int find_max(Tree root) {
while(root->rchild) {
root=root->rchild;
}
return root->val;
}
int find_min(Tree root) {
while(root->lchild) {
root=root->lchild;
}
return root->val;
}
int main() {
Tree root=NULL;
int n;
cin>>n;
while(n--) {
int choose;
cin>>choose;
int val;
int rank;
int now_rank;
Tree pre=NULL;
int e=INF;
int max;
int min;
bool ok;
switch(choose) {
case 1:
cin>>val;
insert(root,val);
break;
case 2:
cin>>val;
remove(root,val);
break;
case 3:
cin>>val;
ok=false;
rank=1;
in_fr(root,val,rank,ok);
cout<<rank<<endl;
break;
case 4:
ok=false;
now_rank=1;
cin>>rank;
in_fx(root,rank,now_rank,ok);
break;
case 5:
cin>>val;
max=find_max(root);
if(val>max) {
cout<<max<<endl;
} else {
rank=1;
ok=false;
find_front(root,val,e,ok,rank);
cout<<e<<endl;
}
break;
case 6:
cin>>val;
min=find_min(root);
if(val<min) {
cout<<min<<endl;
} else {
ok=false;
find_back(root,val,e,ok);
cout<<e<<endl;
}
break;
case 7:
in(root);
}
}
}