#include <bits/stdc++.h>
using namespace std;
int n, m, x, t[1000001];
string s;
queue<int> q[1001];
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> m;
for (int j = 1; j <= m; j++) cin >> x, t[x] = i;
}
while (cin >> s && s != "STOP")
if (s[0] == 'E') {
cin >> x;
if (q[t[x]].empty())
q[0].push(t[x]);
q[t[x]].push(x);
} else {
x = q[0].front();
cout << q[x].front() << endl;
q[x].pop();
if (q[x].empty())
q[0].pop();
}
return 0;
}