#include <bits/stdc++.h>
#define pii pair<int,int>
#define fi first
#define pb push_back
#define si second
#define int  long long
#define mod 1000000007//998244353
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define double long double
#define all(o) o.begin(),o.end()


using namespace std;



int power(int x, int y)
{
    int res = 1LL;
    x = x % mod;
    while (y > 0)
    {
        if (y & 1)
            res = (res*x) % mod;
        y = y>>1; // y = y/2
        x = (x*x) % mod;
    }
    return res%mod;
}
int inv(int  n)
{
    return power(n,mod-2)%mod;
}
//duwmd42rtc
int a[500005];
void solve(){

    int cur=1;
    
    int q;
    cin>>q;
    set<pii> s;
    set<int> t;

    while(q--)
    {
        int op;
        cin>>op;
        if(op==1)
        {
            int m;
            cin>>m;
            a[cur]=m;
            s.insert({m,-cur});
            t.insert(cur);
            cur++;
        }
        else if(op==2)
        {
            auto it=t.begin();
            cout<<*it<<" ";
            s.erase(s.find({a[*it],-*it}));
            t.erase(it);
        }
        else
        {
            auto it=--s.end();
            cout<<-it->si<<" ";
            t.erase(t.find(-it->si));
            s.erase(it);
        }

    }


}
        
int32_t main(){
 fast



    int t=1;
  // cin>>t;
    while(t--) 
    {
        solve();
    }
}