#include<bits/stdc++.h>
using namespace std;
const int N = 1e6 + 7;
const double alpha = 0.7;
struct tree{
int lc, rc;
int tot, val, size;
bool del;
}t[N];
int q;
int order[N], cnt;
int stk[N], top;
int rt;
void inorder(int u){
if(!u) return;
inorder(t[u].lc);
if(!t[u].del) order[++cnt] = u;
else stk[++top] = u;
inorder(t[u].rc);
}
void init(int u){
t[u].lc = t[u].rc = 0;
t[u].size = t[u].tot = 1;
}
void push_up(int u){
t[u].size = t[t[u].lc].size + t[t[u].rc].size + 1;
t[u].tot = t[t[u].lc].tot + t[t[u].rc].tot + 1;
}
void build(int &u, int l, int r){
if(l > r) return;
int mid = (l + r) >> 1;
u = order[mid];
if(l == r){
init(u);
return;
}
if(l < mid) build(t[u].lc, l, mid - 1);
if(l == mid) t[u].lc = 0;
build(t[u].rc, mid + 1, r);
push_up(u);
}
void rebuild(int &u){
cnt = 0;
inorder(u);
if(cnt) build(u, 1, cnt);
else u = 0;
}
bool check(int u){
return (double)max(t[t[u].lc].size, t[t[u].rc].size) <= (double)t[u].size * alpha;
}
void insert(int &u, int x){
if(!u){
u = stk[top--];
t[u].val = x;
init(u);
return;
}
t[u].size++;
t[u].tot++;
if(x < t[u].val) insert(t[u].lc, x);
else insert(t[u].rc, x);
if(!check(u)) rebuild(u);
}
int rk(int u, int x){
if(!u) return 0;
if(x > t[u].val) return t[t[u].lc].size + (t[u].del ^ 1) + rk(t[u].rc, x);
else return rk(t[u].lc, x);
}
int kth(int u, int x){
if(!u) return 0;
if(t[t[u].lc].size + 1 == x) return t[u].val;
if(t[t[u].lc].size < x) return kth(t[u].rc, x - t[t[u].lc].size - 1);
else return kth(t[u].lc, x);
}
void del_k(int u, int k){
t[u].size--;
if(t[t[u].lc].size + 1 == k){
t[u].del = 1;
return;
}
if(k <= t[t[u].lc].size) del_k(t[u].lc, k);
else del_k(t[u].rc, k - t[t[u].lc].size - 1);
}
void del(int u, int x){
del_k(u, rk(u, x));
if(check(u) == 0) rebuild(u);
}
int main(){
scanf("%d", &q);
for(int i = q; i; i--) stk[++top] = i;
while(q--){
int opt, x;
scanf("%d%d", &opt, &x);
if(opt == 1){
insert(rt, x);
}
if(opt == 2){
del(rt, x);
}
if(opt == 3){
printf("%d\n", rk(rt, x) + 1);
}
if(opt == 4){
printf("%d\n", kth(rt, x));
}
if(opt == 5){
printf("%d\n", kth(rt, rk(rt, x)));
}
if(opt == 6){
printf("%d\n", kth(rt, rk(rt, x + 1) + 1));
}
}
return 0;
}