#include<bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
for (int k = 1; k <= t; ++k) {
int n;
cin >> n;
unordered_map<int, int> teamMap;
queue<int> q;
queue<int> teams[1001];
for (int i = 0; i < n; ++i) {
int member;
cin >> member;
teamMap[member] = k;
}
cout << "Scenario #" << k << endl;
string command;
while (cin >> command) {
if (command == "STOP") {
cout << endl;
break;
} else if (command == "ENQUEUE") {
int x;
cin >> x;
int team = teamMap[x];
if (teams[team].empty()) {
q.push(team);
}
teams[team].push(x);
} else if (command == "DEQUEUE") {
int team = q.front();
cout << teams[team].front() << endl;
teams[team].pop();
if (teams[team].empty()) {
q.pop();
}
}
}
}
return 0;
}