/*
1.queue 和 stack 有一些成员函数相似,但在一些情况下,工作方式有些不同:
2.front():返回 queue 中第一个元素的引用。如果 queue 是常量,就返回一个常引用;如果 queue 为空,返回值是未定义的。
3.back():返回 queue 中最后一个元素的引用。如果 queue 是常量,就返回一个常引用;如果 queue 为空,返回值是未定义的。
4.push(const T& obj):在 queue 的尾部添加一个元素的副本。这是通过调用底层容器的成员函数 push_back() 来完成的。
5.push(T&& obj):以移动的方式在 queue 的尾部添加元素。这是通过调用底层容器的具有右值引用参数的成员函数 push_back() 来完成的。
6.pop():删除 queue 中的第一个元素。
7.size():返回 queue 中元素的个数。
8.empty():如果 queue 中没有元素的话,返回 true。
9.emplace():用传给 emplace() 的参数调用 T 的构造函数,在 queue 的尾部生成对象。
10.swap(queue & other_q):将当前 queue 中的元素和参数 queue 中的元素交换。它们需要包含相同类型的元素。也可以调用全局函数模板 swap() 来完成同样的操作。
*/
#include<queue>
#include<string>
#include<stdio.h>
#include<iostream>
using namespace std;
struct node { //运算符<的重载
int priority; int index;
bool operator <(const node &b) const{
if(priority==b.priority)
return index > b.index;
//如果病人的优先级相同,则先来的病人先看病,即Id大的放到后面(逆大根堆),所以用大于号
return priority < b.priority; //优先级不同,则小于号不变
}
};
int main()
{
int n;
priority_queue <node> q[4]; //每个队列里存放一个结构体
while(scanf("%d",&n)!=EOF)
{
int i,x1,x2,t=1;
for(i=1; i<=3; ++i)
while(q[i].size()) q[i].pop();
string str;
for(i=0; i<n; ++i){
cin>>str;
if(str=="IN"){
cin>>x1>>x2;
node temp;
temp.index=t++;
temp.priority=x2;
q[x1].push(temp); //push一个结构体
}
else if(str=="OUT"){
cin>>x1;
if(q[x1].size()){
cout<<q[x1].top().index<<endl;
q[x1].pop();
}
else
cout<<"EMPTY"<<endl;
}
}
}
return 0;
}