#include <bits/stdc++.h>
using namespace std;
int n, idx = 1;
struct node {
int val, rt, lt, lsum, siz;
} t[10050];
void insert(int x) {
if (t[1].siz == 0) {
t[1].val = x;
t[1].siz = 1;
t[1].lsum = 1;
idx++;
return;
}
int i = 1;
while (1) {
if (x == t[i].val) {
t[i].siz++;
t[i].lsum++;
return;
}
if (x > t[i].val) {
if (t[i].rt == 0) {
t[i].rt = idx;
t[idx].siz++;
t[idx].lsum++;
t[idx].val = x;
idx++;
return;
} else {
i = t[i].rt;
}
}
if (x < t[i].val) {
if (t[i].lt == 0) {
t[i].lt = idx;
t[idx].siz++;
t[i].lsum++;
t[idx].lsum++;
t[idx].val = x;
idx++;
return;
} else {
t[i].lsum++;
i = t[i].lt;
}
}
}
}
int rnk(int x, int pos) {
if (pos == 0)
return 0;
if (t[pos].val == x) {
return t[pos].lsum - t[pos].siz;
}
if (t[pos].val < x) {
return rnk(x, t[pos].rt) + t[pos].lsum;
}
if (t[pos].val > x) {
return rnk(x, t[pos].lt);
}
}
int finm(int r, int pos) {
if (pos == 0)
return 2147483647;
if (r > t[pos].lsum) {
return finm(r - t[pos].lsum, t[pos].rt);
}
if (r <= t[pos].lsum && r <= t[pos].lsum - t[pos].siz) {
return finm(r, t[pos].lt);
}
if (r <= t[pos].lsum && r > t[pos].lsum - t[pos].siz) {
return t[pos].val;
}
}
int smaller(int x, int pos, int ans) {
if (x > t[pos].val) {
if (t[pos].rt == 0) {
return t[pos].val;
} else {
return smaller(x, t[pos].rt, t[pos].val);
}
}
if (x <= t[pos].val) {
if (t[pos].lt == 0) {
return ans;
} else {
return smaller(x, t[pos].lt, ans);
}
}
}
int bigger(int x, int pos, int ans) {
if (x < t[pos].val) {
if (t[pos].lt == 0) {
return t[pos].val;
} else {
return bigger(x, t[pos].lt, t[pos].val);
}
} else {
if (t[pos].rt == 0) {
return ans;
} else {
return bigger(x, t[pos].rt, ans);
}
}
}
int read() {
int r = 0;
char ch = getchar();
while (ch < '0' || ch > '9')
ch = getchar();
while (ch >= '0' && ch <= '9') {
r = (r << 3) + (r << 1) + (ch ^ 48);
ch = getchar();
}
return r;
}
int main() {
n = read();
for (int i = 1; i <= n; i++) {
int ins, num;
ins = read();
num = read();
if (ins == 1) {
cout << rnk(num, 1) + 1 << endl;
}
if (ins == 2) {
cout << finm(num, 1) << endl;
}
if (ins == 3) {
cout << smaller(num, 1, -2147483647) << endl;
}
if (ins == 4) {
cout << bigger(num, 1, 2147483647) << endl;
}
if (ins == 5) {
insert(num);
}
}
}
倒数第二个点没过orz