rt
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int inf = 0x3f3f3f3f;
const int mod = 9901;
const int mxn = 1000 + 10;
int t;
int n;
string str;
int mem[mxn*mxn];
int ccc;
int main() {
// freopen("out.out", "w", stdout);
while (cin >> t && t) {
queue <int> umy[mxn];
memset(mem, 0, sizeof mem);
for (int i = 1; i <= t; i++) {
cin >> n;
for (int j = 1; j <= n; j++) {
int x;
cin >> x;
mem[x] = i;
}
}
cout << "Scenario #" << ++ccc << endl;
while (cin >> str && str[0] != 'S') {
if (str[0] == 'E') {
int x;
cin >> x;
umy[mem[x]].push(x);
} else if (str[0] == 'D') {
int cnt = 0;
while (umy[cnt].empty()) {
cnt++;
}
cout << umy[cnt].front() << endl;
umy[cnt].pop();
}
}
cout << endl;
}
return 0;
}