#include <bits/stdc++.h>
using namespace std;
using ll = long long ;
const int N = 1e6 + 50;
int h[N],len;
void down(int u) {
int t = u;
if (2*u <= len&& h[2*u] < h[t]) t = 2*u;
if (2*u+1 <= len && h[2*u+1] < h[t]) t = 2*u;
if (t != u) {
swap(h[t],h[u]);
down(t);
}
}
void up(int u) {
while (u / 2 && h[u] < h[u/2]) {
swap(h[u],h[u/2]);
u /= 2;
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n; cin >> n;
while (n--) {
int op; cin >> op;
int x;
if (op == 1) {
cin >> x;
h[++len] = x;
up(len);
}
else if (op == 2) {
cout << h[1] << '\n';
}
else if (op == 3) {
h[1] = h[len];
len--;
down(1);
}
}
return 0;
}