#include<bits/stdc++.h>
using namespace std;
int nm=1;
typedef struct node {
int r;
int l;
int date;
int size;
int num;
} node;
node tree[10010];
int query1(int x,int root) {
if(x<tree[root].date) {
if(tree[root].l!=0)
return query1(x,tree[root].l);
else
return 1;
}
else if(x==tree[root].date)
return tree[tree[root].l].size+1;
else if(x>tree[root].date)
return tree[tree[root].l].size+tree[root].num+query1(x,tree[root].r);
}
int query2(int x,int root) {
if(x<=tree[tree[root].l].size)
return query2(x,tree[root].l);
else if(x<=tree[tree[root].l].size+tree[root].num)
return tree[root].date;
else
return query2(x-tree[tree[root].l].size-tree[root].num,tree[root].r);
}
int front(int x,int root,int ans){
if(x<=tree[root].date){
if(tree[root].l==0)
return ans;
else
return front(x,tree[root].l,ans);
}
else{
if(tree[root].r==0)
return tree[root].date;
else{
return front(x,tree[root].r,tree[root].date);
}
}
}
int next(int x,int root,int ans){
if(x<tree[root].date){
if(tree[root].l==0)
return tree[root].date;
else
return next(x,tree[root].l,tree[root].date);
}
else{
if(tree[root].r==0)
return ans;
else
return next(x,tree[root].r,ans);
}
}
void insert(int x,int root) {
if(nm==1) {
node t= {0,0,x,1,1};
tree[nm]=t;
nm++;
return;
}
tree[root].size++;
if(x<tree[root].date) {
if(tree[root].l!=0)
insert(x,tree[root].l);
else {
node t= {0,0,x,1,1};
tree[root].l=nm;
tree[nm]=t;
nm++;
}
}
else if(x==tree[root].date) {
tree[root].num++;
return;
}
else if(x>tree[root].date) {
if(tree[root].r!=0)
insert(x,tree[root].r);
else {
node t= {0,0,x,1,1};
tree[root].r=nm;
tree[nm]=t;
nm++;
}
}
}
int main() {
int q;
cin>>q;
for(int i=1;i<=q;i++) {
int op,x;
cin>>op>>x;
if(op==1) {
cout<<query1(x,1)<<endl;
}
if(op==2) {
cout<<query2(x,1)<<endl;
}
if(op==3) {
cout<<front(x,1,-2147483647)<<endl;
}
if(op==4) {
cout<<next(x,1,2147483647)<<endl;
}
if(op==5) {
insert(x,1);
}
}
return 0;
}