/*** akshaykumar99 ***/

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

const long long MOD = 1e9+7;
const long long N = 1e4+1;
 
#define ll int
// typedef long long ll;
typedef long double ld;

void test_case()
{
   ll q;
   cin >> q;
   set<pair<ll,ll>> s2, s3;
   ll curr = 1;
   for(int i = 0; i < q; i++)
   {
      ll x, y;
      cin >> x;
      if(x == 1)
      {
         cin >> y;
         s2.insert({curr, y});
         s3.insert({-y, curr});
         curr++;
      }
      else if(x == 2)
      {
         auto it = s2.begin();
         ll id = (*it).first, inc = (*it).second;
         s2.erase({id, inc});
         s3.erase({-inc, id});
         cout << id << ' ';
      }
      else
      {
         auto it = s3.begin();
         ll inc = (*it).first, id = (*it).second;
         s2.erase({id, -inc});
         s3.erase({inc, id});
         cout << id << ' ';
      }
   }

}
int32_t main()
{
   ios_base::sync_with_stdio(false);
   cin.tie(NULL); cout.tie(NULL); 
   int tc = 1;
   // cin >> tc;
   while(tc--)
      test_case();
   return 0;
}

