// code by a weak man named YJJ
#include <bits/stdc++.h>
#define file(s) freopen(s".in", "r", stdin), freopen(s".out", "w", stdout)
using namespace std;

template <typename T> void read(T &x) {    
  x = 0; int f = 1;    
  char ch = getchar();    
  while (!isdigit(ch)) {    
    if (ch == '-') f = -1;    
    ch = getchar();    
  }    
  while (isdigit(ch)) {    
    x = x * 10 + (ch ^ 48);    
    ch = getchar();    
  }
  return x *= f, void(); 
}

///////////////////////////////////////////////////

const int N = 5e5 + 5;

int q, val[N], cnt, pos = 1;
bool del[N];

struct Data {
  int x;
  inline friend bool operator < (const Data& a, const Data& b) {
    if (val[a.x] != val[b.x]) return val[a.x] < val[b.x];
    return a.x > b.x;
  }
};
priority_queue<Data> Q;

int main() {
  read(q);
  for (int op, x; q; q--) {
    read(op);
    if (op == 1) {
      ++cnt;
      read(val[cnt]);
      Q.push((Data) { cnt });
    } else if (op == 2) {
      while (del[pos]) ++pos;
      del[pos] = true;
      printf("%d\n", pos);
      ++pos;
    } else if (op == 3) {
      while (del[Q.top().x]) {
        Q.pop();
      }
      printf("%d\n", Q.top().x);
      del[Q.top().x] = true;
      Q.pop();
    }
  }
  return 0;
}