#include<iostream>
#include<queue>
using namespace std;
const int N = 1010, M = 1000010;
int teamid[M];
int main()
{int n,C=1; while(cin>>n,n){for(int i=0;i<n;i++){int cnt;cin>>cnt;while(cnt--){int x;cin>>x;teamid[x]=i;}}cout<<"Scenario #"<<C++<<endl;queue<int> team,person[N];string command;while(cin>>command,command!="STOP"){if(command=="ENQUEUE"){int x;cin>>x;int tid=teamid[x];if(person[tid].empty()) team.push(tid);person[tid].push(x); }if(command=="DEQUEUE"){int tid=team.front();queue<int> &q=person[tid];cout<<q.front()<<endl;q.pop();if(q.empty()) team.pop();}}cout<<endl;}return 0;
}