// Problem: E - Sorting Queries
// Contest: AtCoder - AtCoder Beginner Contest 217
// URL: https://atcoder.jp/contests/abc217/tasks/abc217_e
// Memory Limit: 1024 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef unsigned long long ull;
#define x first
#define y second
#define sf scanf
#define pf printf
#define PI acos(-1)
#define inf 0x3f3f3f3f
#define lowbit(x) ((-x)&x)
#define mem(a,x) memset(a,x,sizeof(a))
#define rep(i,n) for(int i=0;i<(n);++i)
#define repi(i,a,b) for(int i=int(a);i<=(b);++i)
#define repr(i,b,a) for(int i=int(b);i>=(a);--i)
#define debug(x) cout << #x <<": " << x << endl;
const int MOD = 998244353;
const int mod = 1e9 + 7;
const int maxn = 2e5 + 10;
const int dx[] = {0, 1, -1, 0, 0};
const int dy[] = {0, 0, 0, 1, -1};
const int dz[] = {1, -1, 0, 0, 0, 0 };
int day[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
void init(){
}
ll n,m;
priority_queue<ll,vector<ll>,greater<ll> > mx;
ll a[maxn];
void solve()
{
cin>>n;
ll l=1,r=0;
for(int i=1;i<=n;i++){
ll op;
cin>>op;
if(op==1){
cin>>m;
a[++r]=m;
}else if(op==2){
if(!mx.empty()){
cout<<mx.top()<<endl;
mx.pop();
}else {
cout<<a[l++]<<endl;
}
}else if(op==3){
for(int i=l;i<=r;i++){
mx.push(a[i]);
}
l=1;
r=0;
}
}
}
int main()
{
init();
ll t = 1;
///scanf("%lld",&t);
while(t--)
{
solve();
}
return 0;
}