#include<iostream>  //2020.12.25
#include<cstdio>
#include<queue>
using namespace std;
struct node{
	int num;
	int value;
	int cal; 
	bool operator<(const node &s)const
	{
		if(s.value!=value)
		return value<s.value;
		else
		return s.num<num;
	}
};
node ar[500005],now;
int q,a,b,cnt;
priority_queue<node>qus;
queue<node>qu;
inline int read()
{
	int x=0,f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9')
	{
		if(ch=='-')
		f=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9')
	{
		x=(x<<1)+(x<<3)+(ch^48);
		ch=getchar();
	}
	return x*f;
}
int main(void)
{
	q=read();
	for(int i=0;i<q;++i)
	{
		a=read();
		if(a==1)
		{
			b=read();
			++cnt;
			ar[cnt].num=cnt;
			ar[cnt].value=b;
			ar[cnt].cal=0;
			qu.push(ar[cnt]);
			qus.push(ar[cnt]);  
		}
		else if(a==2)
		{
			now=qu.front();
			qu.pop();
			while(ar[now.num].cal)
			{
				now=qu.front();
				qu.pop();
			}
			ar[now.num].cal=1;
			cout<<now.num<<endl;
		}
		else if(a==3)
		{
			now=qus.top();
			qus.pop();
			while(ar[now.num].cal)
			{
				now=qus.top();
				qus.pop();
			}
			ar[now.num].cal=1;
			cout<<now.num<<endl;
		}
	}
}
