#include<iostream>
#include<queue>
using namespace std;
int main() {
int i, j;
int t;
int** a;
int* num;
cin >> t;//输入组数
queue<int>* Q;
Q = new queue<int>[t + 3];//创建组队列
num = new int[t];
a = new int* [t];
for (int i = 0; i < t; i++) {//输入样例
cin >> num[i];
a[i] = new int[num[i]];
for (int j = 0; j < num[i]; j++) {
cin >> a[i][j];
}
}
int ct=0;
int* s;
s = new int[t];//建立状态数组表面状态
for (int i = 0; i < t; i++) {//将状态数组置零
s[i] = 0;
}
string c;
int count=0;//定义输入数及计数变量
while (cin >> c) {
if (c == "STOP") {
break;
}
else if (c == "DEQUEUE") {
count++;
}
else if (c == "ENQUEUE") {
int in;
cin >> in;
for (i = 0; i < t; i++) {//确定输入数所在组
for (j = 0; j < num[i]; j++) {
if (in == a[i][j]) {
break;
}
}
if (j != num[i])
break;
}
if (s[i] == 0||i==t) {//若其同组没有在组队列中的
Q[ct].push(in);
ct++;
s[i] = ct;
}
else { //否则加入其同组所在队列末尾
Q[s[i]-1].push(in);
}
}
}
int k=0;
for (i = 0; i < count; i++) {
cout << Q[k].front();
Q[k].pop();
if (i != count - 1)
cout << ' ';
while (Q[k].empty()) {
k++;
}
}
}