> File Name: 5818.cpp
> Author: jiangyuzhu
> Mail: 834138558@qq.com
> Created Time: 2016/8/21 12:32:27
************************************************************************/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<algorithm>
using namespace std;
typedef pair<int,int> p;
priority_queue<p, vector<p>, less<p> >a;
priority_queue<p, vector<p>, less<p> >b;
priority_queue<p, vector<p>, less<p> >c;
char s[10];
char d[10];
int main()
{
int n;int kas = 1;
while(~scanf("%d", &n) && n){
while(!a.empty()) a.pop();
while(!b.empty()) b.pop();
while(!c.empty()) c.pop();
printf("Case #%d:\n", kas++);
char ch;int x;
for(int i = 0; i < n; i++){
scanf("%s", s);
if(s[1] == 'u'){
getchar();
scanf("%c%d", &ch, &x);
if(ch == 'A') a.push(p(i, x));
else b.push(p(i, x));
}else if(s[1] == 'o'){
getchar();
scanf("%c", &ch);
p t;
if(ch == 'A'){
if(a.empty()) t = c.top(), c.pop();
else t = a.top(), a.pop();
}else{
if(b.empty()) t = c.top(), c.pop();
else t = b.top(), b.pop();
}
printf("%d\n", t.second);
}else{
scanf("%s%s", s, d);
while(!a.empty()) c.push(a.top()), a.pop();
while(!b.empty()) c.push(b.top()), b.pop();
}
}
}
return 0;
}