#include<bits/stdc++.h>
using namespace std;
#define ll long long int
int main()
{
    int q,i=1,j=1;
    cin>>q;
    //vector<int> v;
    priority_queue<pair<int,int>> p;
    set<int> s1;
    while(q--)
    {
        int a,b;
        cin>>a;
        if(a==1)
        {
            cin>>b;
            //s.insert(make_pair(i,b));
            p.push(make_pair(b,-i));
            s1.insert(i);
            i++;
        }
        else if(a==2)
        {


            cout<<*s1.begin()<<" ";
            s1.erase(s1.begin());
            //j++;

        }
        else
        {
            while(s1.find(-p.top().second)==s1.end())
                p.pop();
            cout<<-p.top().second<<" ";
            s1.erase(-p.top().second);
            p.pop();

        }
    }

}
