Queues and priority queues are data structures familiar to most computer scientists. The team queue, however, is less well known even though it appears often in everyday life. For example, the line in front of the cafeteria at lunch time is a team queue.
In a team queue, every element belongs to a team. When a new element enters the queue, it first scans the queue from front to back to see whether any of its teammates (elements of the same team) are already present. If so, it enters the queue immediately behind them. If not, it joins the queue at the tail and becomes the new last element. Dequeuing works as in an ordinary queue: elements are processed from front to back in the order they occupy the team queue.
Write a program that simulates such a team queue.
The input consists of one or more test cases. Each test case begins with the number of teams $t$ ($1 \le t \le 1000$). Then $t$ team descriptions follow; each description consists of the number of elements in the team followed by those elements. Elements are integers in the range $0$ to $999999$, and a team may contain up to $1000$ elements.
A list of commands then follows. There are three kinds of commands:
ENQUEUE x — put element $x$ into the team queue.DEQUEUE — process the front element and remove it from the queue.STOP — end of the test case.The input ends when $t$ is $0$.
Note: a single test case may contain up to 200000 commands, so the team queue must be implemented efficiently — both enqueuing and dequeuing should take constant time.
For each test case, first print a line Scenario #k, where $k$ is the number of the test case (starting from $1$). Then, for each DEQUEUE command, print the dequeued element on its own line. Separate the outputs of consecutive test cases with a single blank line.