/*
队列操作, 用了一个1000000的数组记录每个人所属team
*/
#include <cstdio>
#include <cstring>
#include <cctype>
#include <algorithm>
#include <queue>
using namespace std;
const int MAXT = 1004;
const int HASH_SIZE = 1000004;
const int BUFF_SIZE = 16;
int vis[HASH_SIZE];
char buff[BUFF_SIZE];
queue<int> q[MAXT];
queue<int> teams;
int t;
void init()
{
while(!teams.empty()) teams.pop();
for(int i=1; i<=t; i++) {
while(!q[i].empty()) q[i].pop();
}
}
void inq(int n)
{
int i = vis[n];
if(q[i].size() == 0) {
teams.push(i);
}
q[i].push(n);
}
int outq()
{
int i = teams.front();
int n = q[i].front();
q[i].pop();
if(q[i].size() == 0) {
teams.pop();
}
return n;
}
int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif
int n, k, T=0;
while(scanf("%d", &t) == 1 && t) {
for(int i=1; i<=t; i++) {
scanf("%d", &k);
for(int j=0; j<k; j++) {
scanf("%d", &n);
vis[n] = i;
}
}
printf("Scenario #%d\n", ++T);
init();
while(scanf("%s", buff)) {
if(buff[0] == 'S') break;
if(buff[0] == 'E') {
scanf("%d", &n);
inq(n);
} else {
printf("%d\n", outq());
}
}
printf("\n");
}
return 0;
}