#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <stack>
#include <string>
#define ll long long
using namespace std;
#define INF 0x3f3f3f3f
#define pii pair<int,int>
const int N = 1e6 + 10;
int h[N], cnt;
int n;
void down(int u) {
int t = u;
if(u * 2 <= cnt && h[u * 2] < h[t])
t = u * 2;
if(u * 2 + 1 <= cnt && h[u * 2 + 1] < h[t])
t = u * 2 + 1;
if(t != u) {
swap(h[t], h[u]);
down(t);
}
}
int main() {
ios::sync_with_stdio(false); cin.tie(0);
cin >> n;
while(n--) {
int op;
cin >> op;
if(op == 1) {
int x;
cin >> x;
h[++cnt] = x;
for(int i = cnt / 2; i; --i) down(i);
} else if(op == 2) {
cout << h[1] << "\n";
} else {
h[1] = h[cnt--];
down(1);
}
}
return 0;
}