#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define sll static long long int
#define pb push_back
#define L1(d,n) for(ll i=d;i<n;i++)
#define L2(d,n) for(ll j=d ; j>=n ; j--)
#define _mp make_pair
#define all(x) x.begin(),x.end()
#define SORT(a) sort(a.begin(),a.end())
#define nl  "\n"
#define ok  cout<<"OK\n"
#define ios ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define free freopen("input.txt","r",stdin);freopen("output.txt","w",stdout)
#define mod 1000000007
typedef vector<ll> vl;
typedef stack<ll> S;
typedef queue<ll> Q;
const long long INF = 1ll << 32;

bool cmp2(pair<ll,pair<ll,ll> > &a, pair<ll,pair<ll,ll> > &b)
{
    if(a.second.first !=b.second.first)
    return a.second.first <b.second.first ;
    else return a.second.second <b.second.second;
}
bool cmp3(pair<ll,pair<ll,ll> > &a, pair<ll,pair<ll,ll> > &b)
{
    if(a.second.second !=b.second.second)
    return a.second.second <b.second.second ;
    else
        a.second.first < b.second.first ;
}
ll sz(ll n)
{
    ll x=1;
    while(n>1)
    {
        n/=2;
        x*=2;
    }
    return x;
}
vl v;

ll divide(ll x,ll n,ll i)
{
    if(n<2)
        return n;
    if(i*2==x+1)
        return n%2;
    x/=2;
    n/=2;
    if(i>x)
        i-=(x+1);
    return divide(x,n,i);
}


void solve()
{
    ll n,q;
    ll cust=0;
    multimap<ll,ll> mn;
    multimap<ll,ll,greater<ll> > po;
    cin>>q;
    while(q--)
    {
        cin>>n;
        if(n==1)
        {
            ll p;
            cin>>p;
            cust++;
            mn.insert({cust,p});
            po.insert({p,cust});
        }
        else if(n==2)
        {
            auto it=mn.begin();
            cout<<it->first<<" ";
            mn.erase(it->first);
            it = po.find(it->second);
            po.erase(it);
        }
        else if(n==3)
        {
            auto it=po.begin();
            cout<<it->second<<" ";
            mn.erase(it->second);
            po.erase(it);
        }
    }






}
int main()
{
    ios;
    ll t;
    t=1;
    while(t--)
    {
        solve();
    }





















}
