#include using namespace std; queue bigqueue; queue que[1000]; int team[1000000]; int main() { int TeamN; int FactorN; int i, temp; int num = 0; string s; int flag = 0; while (cin >> TeamN && TeamN) { while (!bigqueue.empty()) bigqueue.pop(); for (i = 0; i < 1000; i++) while (!que[i].empty()) que[i].pop(); num = 0; while (TeamN--) { num++; cin >> FactorN; for (i = 0; i < FactorN; i++) { cin >> temp; team[temp] = num; } } printf("Scenario #%d\n", ++flag); while (cin >> s && s[0] != 'S') { if (s == "ENQUEUE") { cin >> num; if (que[team[num]].empty()) bigqueue.push(team[num]); que[team[num]].push(num); } else if (s == "DEQUEUE") { printf("%d\n", que[bigqueue.front()].front()); que[bigqueue.front()].pop(); if (que[bigqueue.front()].empty()) bigqueue.pop(); } } if (TeamN) printf("\n"); } return 0; }