bool operator < (const node &b) const {
return this->val < b.val;
}
//#pragma GCC optimize(2)
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <cctype>
#include <string>
#include <cstring>
#include <algorithm>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <ctime>
#include <vector>
#include <fstream>
#include <list>
#include <iomanip>
#include <numeric>
using namespace std;
typedef long long ll;
const int MAXN = 1e6 + 10;
struct node
{
int val, num;
node(int a, int b)
{
val = a, num = b;
}
bool operator < (const node &b) const
{
if(val != b.val)
return val < b.val;
else
return num > b.num;
}
};
int main()
{
//ios::sync_with_stdio(false);
//cin.tie(0); cout.tie(0);
int N, x, y, flag;
while(cin>>N)
{
priority_queue <node> DOC[4];
flag = 1;
while(N--)
{
string oprt;
cin>>oprt;
if(oprt == "IN")
{
cin>>x>>y;
DOC[x].push(node(y, flag++));
}
else
{
cin>>x;
if(DOC[x].empty())
cout<<"EMPTY"<<endl;
else
{
node t = DOC[x].top();
DOC[x].pop();
cout<<t.num<<endl;
}
}
}
}
return 0;
}