#include<bits/stdc++.h>
#define lld long long int
#define ulld unsigned long long int
typedef long long ll;
using namespace std;
struct comp
{
  bool operator()(pair<int,int>p1, pair<int,int>p2)
{
      if(p1.first != p2.first)
              return p1.first < p2.first;
      else
              return p1.second> p2.second;
}
};
void solve(){
   lld n;
   cin>>n;
   set<pair<int,int>,comp> poly,mono; 
   vector<int> ans;
   lld time=1;
   while(n--)
   {
     int a,b;
     cin>>a;
     if(a==1)
     {
       cin>>b;
       mono.insert({time,b});
       poly.insert({b,time});
       time++;
     }
     else if(a==2)
     {
       auto p=*(mono.begin());
       mono.erase(p);
       cout<<p.first<<" ";
       poly.erase({p.second,p.first});
     }
     else if(a==3)
     {
       auto p=*(poly.rbegin()); 
       poly.erase(p);
       cout<<p.second<<" ";
       mono.erase({p.second,p.first});
     }
   }
  }
int main(){
   ios::sync_with_stdio(0);
   cin.tie(0);
   ll t;
   t=1;
   while(t--)
   { 
   solve();
   }
   return 0;
}