#include<iostream>
#include<queue>
using namespace std;
struct ss
{
friend bool operator<(const ss a,const ss b)
{
if(a.v<b.v)
return 1;
else
if(a.v==b.v&&a.num>b.num)
return 1;
else
return 0;
}
int num,v;
};
int main()
{
int n;
while(scanf("%d",&n)>0)
{
ss t;
priority_queue<ss>Q[4];
int i,j=0;
char s[5];
for(i=1;i<=n;i++)
{
scanf("%s",s);
if(s[0]=='I')
{
int x,y;
j++;
scanf("%d%d",&x,&y);
t.num=j;
t.v=y;
Q[x].push(t);
}
else
{
int x;
scanf("%d",&x);
if(Q[x].empty())
{
printf("EMPTY\n");
}
else
{
printf("%d\n",Q[x].top().num);
Q[x].pop();
}
}
}
}
return 0;
}