#include <bits/stdc++.h>
using namespace std;
int p_in_gid[1000005];
int gc[1005];
int main(){
int n, l = 1;
while(cin >> n){
list<int> line;
list<int>::iterator g_end[1000];
memset(gc, 0, sizeof(g_end));
memset(p_in_gid, -1, sizeof(p_in_gid));
for(int i = 0, m; i < n; i++){
cin >> m;
for(int j = 0, p; j < m; j++){
cin >> p;
p_in_gid[p] = i;
}
}
cout << "Line #" << l++ << '\n';
string str;
while(cin >> str, str[0] != 'S'){
if(str[0] == 'E'){
int id;
cin >> id;
int i = p_in_gid[id];
if(i == -1){
line.emplace_back(id);
}else if(gc[i] == 0){
line.emplace_back(id);
g_end[i] = --line.end();
gc[i]++;
}else{
gc[i]++;
g_end[i] = line.insert(++g_end[i], id);
}
}else if(str[0] == 'D'){
cout << line.front() << '\n';
int i = p_in_gid[line.front()];
if(i > -1) gc[i]--;
line.erase(line.begin());
}
}
}
}