#include <bits/stdc++.h>
using namespace std;
const char el = '\n';
using ll = long long;

int teamOf[1000000];

int main() {
    cin.tie(0)->sync_with_stdio(0);

    int t;
    int scenario = 1;
    while (cin >> t && t != 0) {
        for (int i = 0; i < t; i++) {
            int n;
            cin >> n;
            for (int j = 0; j < n; j++) {
                int x;
                cin >> x;
                teamOf[x] = i;
            }
        }
        queue<int> teamQ;
        queue<int> q[1000];
        bool inQ[1000] = {};
        cout << "Scenario #" << scenario++ << el;
        string c;
        while (cin >> c && c != "STOP") {
            if (c == "ENQUEUE") {
                int x;
                cin >> x;
                int team = teamOf[x];
                if (!inQ[team]) {
                    teamQ.push(team);
                    inQ[team] = true;
                }
                q[team].push(x);
            }
            else if (c == "DEQUEUE") {
                int team = teamQ.front();
                cout << q[team].front() << el;
                q[team].pop();
                if (q[team].empty()) {
                    teamQ.pop();
                    inQ[team] = false;
                }
            }
        }
    }
    return 0;
}