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

const long long int mod = 1000000007;
const long long int big = 2999999999999999999;
const long long int small = -2999999999999999999;
#define ll long long int 
#define pb(a) push_back(a)
#define vll vector<long long int>
#define loop(i, n) for(long long int i=1;i<=n;i++)
#define loop0(i, n) for(long long int i=0;i<n;i++)
#define in(i) scanf("%lld", &i);
#define out(i) printf("%lld", i)
#define pll pair<ll, ll>
#define vpll vector<pair<ll, ll>>

ll ch, m, c;
set<pll> orderByPrice, order;

void in2(ll& a, ll& b)
{
  in(a);
  in(b);
}
void in3(ll& a, ll& b, ll& c)
{
  in(a);
  in(b);
  in(c);
}

int main()
{
  ll t;
  in(t);
  while(t--)
  {
    in(ch);
    switch(ch)
    {
      ll a, b;
      case 1 : in(m);
               c += 1;
               order.insert({c, m});
               orderByPrice.insert({-m, c});
               break;
      case 2 : a = order.begin()->first, b = order.begin()->second;
               cout<<a<<" ";
               orderByPrice.erase({-b, a});
               order.erase({a, b});
               break;
      case 3 : b = -orderByPrice.begin()->first, a = orderByPrice.begin()->second;
               cout<<a<<" ";
               orderByPrice.erase({-b, a});
               order.erase({a, b});
               break;
    }
  }
}

