#include <bits/stdc++.h>
#define fastios ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define endl '\n'
using namespace std;
#define int long long
const int N = 1e6 + 10;
int d[N], ssize;
void down(int u) {
int t = u;
if (2 * t <= ssize && d[2 * t] < d[t])
t = 2 * t;
if (2 * t + 1 <= ssize && d[2 * t + 1] < d[t])
t = 2 * t + 1;
if (u != t) {
swap(d[u], d[t]);
down(t);
}
}
signed main() {
int t;
cin >> t;
while (t--) {
int op;
cin >> op;
if (op == 1) {
ssize++;
cin >> d[ssize];
}
for (int i = ssize / 2; i >= 1; i--)
down(i);
if (op == 2 && ssize > 0)
cout << d[1] << endl;
if (op == 3 && ssize > 0) {
d[1] = d[ssize];
ssize--;
down(1);
}
}
return 0;
}