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

int main()
{
    int q;
    cin>>q;
    set<int> s2;
    map<int, int> mp;
    set<pair<int, int>> s;
    int sz=0;
    while(q--)
    {
        int type, m;
        cin>>type;
        if(type==1) 
        {
            cin>>m;
            mp[sz]=m;
            s.insert({m, -sz});
            s2.insert(sz);
            sz++;
        }
        if(type==2)
        {
            auto i=*s2.begin();
            cout<<i+1<<" ";
            s.erase({mp[i], -i});
            s2.erase(i);
        }
        if(type==3)
        {
            auto last = *s.rbegin();
            //cout<<last.first<<" ";
            cout<<-last.second+1<<" ";
            s2.erase(-last.second);
            s.erase(last);
            //cout<<*s2.begin()<<" "<<s.size()<<endl;
        }
    }
    cout<<endl;
}