// 别人的代码
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <string>
#define MAXN 1000010
#define MAXT 10000
using namespace std;
typedef struct List{
int date;
struct List *next;
}List;
struct Queue{
List *head,*tail;
}Q[1001] = {0};
int T,n,team[MAXN] = {0},whichteam[MAXT] = {0};
void Input(){
memset(team,0,sizeof(team));
int element;
for(int t = 0;t < T;t++){
scanf("%d",&n);
for(int i = 0;i < n;i++){
scanf("%d",&element);
team[element] = t;
}
}
}
void Solve(){
List *p;
char opr[20];
int num = 0,pos = 0,temp,flage,E;
memset(whichteam,-1,sizeof(whichteam));
while(scanf("%s",opr) && opr[0] != 'S'){
if(opr[0] == 'E'){
scanf("%d",&E);
p = (List *)malloc(sizeof(List));
p->date = E;
p->next = NULL;
if(whichteam[team[E]] == -1){
whichteam[team[E]] = num;
Q[num].head = Q[num].tail = p;
num++;
}
else{
temp = whichteam[team[E]];
Q[temp].tail->next = p;
Q[temp].tail = p;
}
}
else{ /*output the elementary*/
if(!Q[pos].head){
pos++;
whichteam[flage] = -1;
}
if(Q[pos].head){
printf("%d/n",Q[pos].head->date);
flage = team[Q[pos].head->date];
Q[pos].head = Q[pos].head->next;
}
}
}
printf("/n");
}
int main()
{
int t = 0;
while(scanf("%d",&T) && T != 0 ){
printf("Scenario #%d/n",++t);
Input();
Solve();
}
return 0;
}