A team queue works almost the same as the ordinary queue, with a little difference: When an element enters a queue, it first searches the queue to check if some of its teammates (elements of the same team) are already in the queue. If yes, it enters the queue right behind its teammates. If not, it enters the queue at the tail and becomes the new last element. Dequeuing is the same as the ordinary one: The elements are popped in the order they appear in the team queue. Given the team descriptions and a sequence of commands, simulate such a queue.
The first line contains a single integer T (T ≤ 10) which represents the number of test cases. Each test case starts with two integers n, m (n ≤ 500, m ≤ 50000) denoting the number of teams and operations in this test case. The next n lines describe the teams, one team per line. The first integer k (k ≤ 500) in each line is the number of teammates in the team, and the following integers are the IDs of the team members. After that, next m lines describe m operations, one operation per line. You may assume that the ID will fit in a 32-bit signed integer.
For each test case, first print a line containing “Case i:”, where i is the number of the test case, starting from 1. For each POP operation, print the popped number on a single line. Print a blank line between test cases.