#include<bits/stdc++.h>
using namespace std ;

typedef long long int ll;
typedef vector<ll>vi;
typedef pair<ll,ll>pi;

const ll INF = 1e18;
const ll MOD = 1e9 + 7;

#define pb push_back
#define ff first
#define ss second
#define mkp make_pair

#define dbs cout<<"Debug\n";
#define dbar(arr,n){for(int i=0;i<n;i++) cout<<arr[i]<<" ";} cout<<'\n';

#define fl(i,n) for(ll i=0;i<n;++i)
#define fls(i,l,n) for(ll i=l;i<n;++i)
#define flb(i,l,n) for(ll i=l;i>=n;--i)
#define al(z, x) for(auto x : z)
#define alb(z, x) for(auto x = z.rbegin(); x!= z.rend(); ++x)
#define mems(z,k) memset(z, k, sizeof(z))
#define findin(a, b) (a.find(b)!=a.end()?true:false)

#define maxn (ll)1e6+7

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll TT=1; //cin>>TT;
    while(TT--)
    {
        ll n,m,k=0;
        ll tc; cin>>tc;
        ll x,y;
        ll a,b;
        map<ll,ll>cust;
        map<ll, set<ll>>vals;
        set<ll>temp;
        ll idx = 0;
        while(tc--)
        {
            cin>>x;
            if(x == 1)
            {
                cin>>y;
                idx++;
                cust[idx] = y;
                vals[y].insert(idx);
            }
            else if(x == 2)
            {
                auto it = cust.begin();
                a = it->ff; b = it->ss;
                cout<<a<<" ";
                cust.erase(cust.find(a));
                vals[b].erase(a);
                if(vals[b].empty()) vals.erase(vals.find(b));
            }
            else
            {
                auto it = vals.rbegin();
                a = it->ff;
                b = *(it->ss.begin());
                cout<<b<<" ";
                it->ss.erase(it->ss.find(b));
                cust.erase(cust.find(b));
                if(it->ss.empty()) vals.erase(a);
            }
        }
    }
    return 0;
}
