#include <iostream>
#include <algorithm>
#include <cstring>
#include <set>
using namespace std;
multiset<int> st;
int n;
int op, x;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> op >> x;
if (op == 1) {
auto it = st.lower_bound(x);
int tmp = 1;
for (auto i = st.begin(); i != it; i++)
tmp++;
cout << tmp << endl;
} else if (op == 2) {
int tmp = 1;
for (auto it = st.begin(); it != st.end(); it++) {
if (tmp == x) {
cout << *it << endl;
break;
}
tmp++;
}
} else if (op == 3) {
if (!st.count(x)) {
cout << "-2147483647" << endl;
continue;
}
auto it = st.lower_bound(x);
cout << *(--it) << endl;
} else if (op == 4) {
if (!st.count(x)) {
cout << "2147483647" << endl;
continue;
}
for (auto it = st.begin(); it != st.end(); it++) {
if (*it == x) {
cout << *(++it) << endl;
break;
}
}
} else if (op == 5) {
st.insert(x);
}
}
return 0;
}