#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 10;
int q, root, cnt, op, x;
struct Tree {
int left, right, size, value, num;
Tree(int l, int r, int s, int v) {
left = l, right = r, size = s, value = v, num = 1;
}
Tree() {}
} tree[MAXN];
inline void update(int root) {
tree[root].size = tree[tree[root].left].size + tree[tree[root].right].size + tree[root].num;
}
inline int rnk(int x, int root) {
if (root) {
if (x < tree[root].value) {
return rnk(x, tree[root].left);
}
if (x > tree[root].value) {
return rnk(x, tree[root].right) + tree[tree[root].left].size + tree[root].num;
}
return tree[tree[root].left].size + tree[root].num;
}
return 1;
}
inline int kth(int x, int root) {
if (x <= tree[tree[root].left].size) {
return kth(x, tree[root].left);
}
if (x <= tree[tree[root].left].size + tree[root].num) {
return tree[root].value;
}
return kth(x - tree[tree[root].left].size - tree[root].num, tree[root].right);
}
inline void insert(int x, int &root) {
if (x < tree[root].value) {
if (!tree[root].left) {
tree[tree[root].left = ++cnt] = Tree(0, 0, 1, x);
} else {
insert(x, tree[root].left);
}
} else if (x > tree[root].value) {
if (!tree[root].right) {
tree[tree[root].right = ++cnt] = Tree(0, 0, 1, x);
} else {
insert(x, tree[root].right);
}
} else {
tree[root].num++;
}
update(root);
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> q;
int num = 0;
tree[root = ++cnt] = Tree(0, 0, 1, INT_MAX);
while (q--) {
cin >> op >> x;
num++;
if (op == 1) {
cout << rnk(x, root) << '\n';
} else if (op == 2) {
cout << kth(x, root) << '\n';
} else if (op == 3) {
cout << kth(rnk(x, root) - 1, root) << '\n';
} else if (op == 4) {
cout << kth(rnk(x + 1, root), root) << '\n';
} else {
num--, insert(x, root);
}
}
return 0;
}