#include <bits/stdc++.h>
#define rc(x) return cout<<x<<endl,0
#define pb push_back
#define mkp make_pair
#define in insert
#define er erase
#define fd find
#define fr first
#define sc second
#define all(x) x.begin(),x.end()
#define lun(x) (int)x.size()
typedef long long ll;
typedef long double ld;
const ll INF=0x3f3f3f3f3f3f3f3f;
const ll llinf=(1LL<<60);
const int inf=(1<<30);
const int nmax=5e5+50;
const ll mod=1e9+7;
using namespace std;
int q,tp,x,v[nmax],id;
set<int>t;
set<pair<int,int> >s;
set<int>::iterator it;
set<pair<int,int> >::iterator it1;
pair<int,int>p;
int main()
{
    //freopen("sol.in","r",stdin);
    //freopen("sol.out","w",stdout);
    //mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
    ios_base::sync_with_stdio(false);cin.tie(0);cerr.tie(0);cout.tie(0);
    cin>>q;
    while(q--)
    {
        cin>>tp;
        if(tp==1)
        {
            cin>>x;
            id++;
            s.in(mkp(x,-id));
            t.in(id);
        }
        else if(tp==2)
        {
            while(1)
            {
                it=t.begin();
                x=*it;
                t.er(it);
                if(!v[x])
                {
                    v[x]=1;
                    break;
                }
            }
            cout<<x<<" ";
        }
        else
        {
            while(1)
            {
                it1=--s.end();
                p=*it1;
                s.er(it1);
                if(!v[-p.sc])
                {
                    v[-p.sc]=1;
                    break;
                }
            }
            cout<<-p.sc<<" ";
        }
    }
    cout<<'\n';
    return 0;
}
