#include<iostream>
#include<queue>
using namespace std;
struct node
{
int x;
int y;
friend operator < (node a, node b)
{
if(a.x==b.x)
return a.y>b.y;
return a.x<b.x;
}
};
int main()
{
int n;
while(~scanf("%d",&n))
{
string s;node patient;
int t=1,a,b,x;
priority_queue<node>q[4];
for(int i=1;i<=n;i++)
{
cin>>s;
if(s=="IN")
{
scanf("%d%d",&a,&b);
patient.x=b;
patient.y=t++;
q[a].push(patient);
}
else
{
scanf("%d",&x);
if(q[x].empty()) puts("EMPTY");
else
{
patient=q[x].top();
q[x].pop();
printf("%d\n",patient.y);
}
}
}
}
return 0;
}