#include <bits/stdc++.h>
using namespace std;
map<int, int> fa;
map<int, queue<int>> h;
queue<int> z;
int find(int x) {
if (x == fa[x])return x;
else return fa[x] = find(fa[x]);
}
int work(int T) {
int t;
scanf("%d", &t);
if(t == 0){
exit(0);
}
printf("Scenario #%d\n", T);
while (--t) {
int n;
scanf("%d", &n);
int x;
scanf("%d", &x);
fa[x] = x;
for (int i = 1; i < n; ++i) {
int y;
scanf("%d", &y);
fa[y] = x;
}
string s;
while(cin >> s){
if(s == "ENQUEUE"){
int x;
scanf("%d", &x);
h[find(x)].push(x);
if (h[fa[x]].size() == 1){
z.push(fa[x]);
}
}
if(s == "DEQUEUE"){
int head = z.front();
queue<int>& q = h[head];
while(!q.empty()){
int v = q.front();
q.pop();
printf("%d\n", v);
}
}
if(s == "STOP"){
return 0;
}
}
}
return 0;
}
int main(){
for (int i = 1; ; ++i)work(i);
return 0;
}