#include<stdio.h> #include<string.h> #include<algorithm> #include<queue> using namespace std; int t; struct node { int rank,num; friend bool operator < (node a,node b) { if(a.rank == b.rank) return a.num > b.num; return a.rank < b.rank; } }; void bfs() { char a[5]; int i = 1,n,m; priority_queue <node> q[4]; node s; while(t--) { scanf("%s",a); if(strcmp(a,"IN") == 0) { scanf("%d%d",&n,&m); s.rank = m; s.num = i++; q[n].push(s); } else { scanf("%d",&n); if(q[n].empty()) printf("EMPTY\n"); else { s = q[n].top(); q[n].pop(); printf("%d\n",s.num); } } } } int main() { while(~scanf("%d",&t)) { bfs(); } return 0; }