Student's queue in a canteen
In ADA University students like Programming competitions very much, so each student belongs to one (and only one) team. But the rules of different competitions are different, and not usually one team consists of members like according to ICPC rules. Any team can contain any number of students (but of course more than ).
Students like to come to their canteen which is a building C and spent their free time with a cup of coffee. Students in ADA are very smart, they do not want to stand in standard queue for delicious coffee. They decided to establish some rules that only they would follow.
If a student enters the queue, he first searches the queue from head to tail to check if some of his teammates (student of the same team) are already in the queue. If yes, he enters the queue right behind them (behind all his teammates). If not, he enters the queue at the tail and becomes the new last element (bad luck). Dequeuing is done like in normal queues: students are processed from head to tail in the order they appear in the queue.
Your task is to write a program that simulates such a queue.
Input
First line contains number of teams . Each of the next lines describes one team. First element in the line is the number of students in a team. Next integers in a line give the ID's of students in a team.
Finally, a list of commands follows. There are two different kinds of commands:
ENQUEUE x — student enters into the queue
DEQUEUE — process the first student and remove him from the queue
Output
For each DEQUEUE command print the element which is dequeued on a single line.