#include<cstdio>
#include<queue>
#include<map>
using namespace std;
int main(){
// freopen("input.txt","r",stdin);
int t,k=0,n,temp;
while(scanf("%d",&t)!=EOF){
map<int,int>team;
queue<int> q,q1[1010];
if(t==0)break;
k++;
for(int i=1 ;i<=t ;i++){
scanf("%d",&n);
while(n--){
scanf("%d",&temp);
team[temp] = i;
}
}
char s[10];
printf("Scenario #%d\n",k);
while(scanf("%s",s)!=EOF){
if(s[0]=='E'){
scanf("%d",&temp);
int t = team[temp];
if(q1[t].empty()){
q.push(t);
}
q1[t].push(temp);
}
if(s[0]=='D'){
int t = q.front();
printf("%d\n",q1[t].front());
q1[t].pop();
if(q1[t].empty()){
q.pop();
}
}
if(s[0]=='S'){
printf("\n");
break;
}
}
}
return 0;
}