#include <queue>
#include <cstdio>
using namespace std;
struct node
{
int id,lv;
friend bool operator < (const node x,const node y)
{
if(x.lv==y.lv)
{
return x.id>y.id;
}
return x.lv<y.lv;
}
}tmp;
int main()
{
char str[5];
int n,i,a,b;
while(~scanf("%d",&n))
{
tmp.id=0;
priority_queue<node> q[4];
for(i=1;i<=n;i++)
{
scanf("%s",str);
if(str[0]=='I')
{
tmp.id++;
scanf("%d%d",&a,&b);
tmp.lv=b;
q[a].push(tmp);
}else{
scanf("%d",&a);
if(!q[a].empty())
{
printf("%d\n",q[a].top().id);
q[a].pop();
}else{
puts("EMPTY");
}
}
}
}
return 0;
}
#include <cstdio>
using namespace std;
struct node
{
int id,lv;
friend bool operator < (const node x,const node y)
{
if(x.lv==y.lv)
{
return x.id>y.id;
}
return x.lv<y.lv;
}
}tmp;
int main()
{
char str[5];
int n,i,a,b;
while(~scanf("%d",&n))
{
tmp.id=0;
priority_queue<node> q[4];
for(i=1;i<=n;i++)
{
scanf("%s",str);
if(str[0]=='I')
{
tmp.id++;
scanf("%d%d",&a,&b);
tmp.lv=b;
q[a].push(tmp);
}else{
scanf("%d",&a);
if(!q[a].empty())
{
printf("%d\n",q[a].top().id);
q[a].pop();
}else{
puts("EMPTY");
}
}
}
}
return 0;
}