#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
int root, tot, n, fa[maxn], ch[maxn][2], val[maxn], cnt[maxn], siz[maxn];
inline int read(){
int x = 0, f = 1;
char ch = getchar();
while(ch < '0' or ch > '9') {if(ch == '-') f = -1; ch = getchar();}
while(ch >= '0' and ch <= '9') {x = (x << 1) + (x << 3) + (ch ^ 48); ch = getchar();}
return x * f;
}
inline void write(int x, int dis){
if(x < 0) putchar('-'), x = -x;
if(x > 9) write(x / 10, 0);
putchar(x % 10 + '0');
if(dis == 1) putchar('\n');
return;
}
struct Splay{
bool get(int x) {return ch[fa[x]][1] == x;}
void clear(int x) {ch[x][0] = ch[x][1] = fa[x] = val[x] = siz[x] = cnt[x] = 0;}
void update(int x) {siz[x] = siz[ch[x][0]] + siz[ch[x][1]] + cnt[x];}
void rotate(int x){
int f = fa[x], g = fa[f], son = get(x);
ch[f][son] = ch[x][son ^ 1];
if( ch[x][son ^ 1]) fa[ch[x][son ^ 1]] = f;
ch[x][son ^ 1] = f; fa[f] = x; fa[x] = g;
if( g) ch[g][f == ch[g][1]] = x;
update(f); update(x);
}
void splay(int x){
for(int f = fa[x]; f = fa[x], f; rotate(x))
if( fa[f]) rotate(get(x) == get(f) ? f : x);
root = x;
}
void Insert(int k){
if(!root) {val[++tot] = k; cnt[tot]++; root = tot; update(root); return;}
int cur = root, f = 0;
while(true){
if(val[cur] == k) {cnt[cur]++; update(cur); update(f); splay(cur); break;}
f = cur; cur = ch[cur][val[cur] < k];
if(!cur){
val[++tot] = k; cnt[tot]++; fa[tot] = f; ch[f][val[f] < k] = tot;
update(tot); update(f); splay(tot); break;
}
}
}
int rk(int k){
int cur = root, res = 0;
while(true){
if(k < val[cur]) cur = ch[cur][0];
else{
res += siz[ch[cur][0]];
if(k == val[cur]) {splay(cur); return res;}
res += cnt[cur]; cur = ch[cur][1];
}
}
}
int kth(int k){
int cur = root;
while(true){
if(ch[cur][0] and k <= siz[ch[cur][0]]) cur = ch[cur][0];
else{
k -= cnt[cur] + siz[ch[cur][0]];
if(k <= 0) {splay(cur); return val[cur];}
cur = ch[cur][1];
}
}
}
int pre(){
int cur = ch[root][0];
if(!cur) return cur;
while(ch[cur][1]) cur = ch[cur][1];
splay(cur);
return cur;
}
int nxt(){
int cur = ch[root][1];
if(!cur) return cur;
while(ch[cur][0]) cur = ch[cur][0];
splay(cur);
return cur;
}
void del(int k){
rk(k);
if(cnt[root] > 1) {cnt[root]--; update(root); return;}
if(!ch[root][0] and !ch[root][1]) {clear(root); root = 0; return;}
if(!ch[root][0]) {int cur = root; root = ch[root][1]; fa[root] = 0; clear(cur); return;}
if(!ch[root][1]) {int cur = root; root = ch[root][0]; fa[root] = 0; clear(cur); return;}
int cur = root, x = pre(); fa[ch[cur][1]] = x; ch[x][1] = ch[cur][1]; clear(cur); update(root);
}
}tree;
void Solve(){
n = read();
while(n--){
int opt = read(), x = read();
if(opt == 1) tree.Insert(x);
if(opt == 2) tree.del(x);
if(opt == 3) write(tree.rk(x) + 1, 1);
if(opt == 4) write(tree.kth(x), 1);
if(opt == 5) tree.Insert(x), write(val[tree.pre()], 1), tree.del(x);
if(opt == 6) tree.Insert(x), write(val[tree.nxt()], 1), tree.del(x);
}
}
int main(){
Solve();
return 0;
}