Opening the Treasure Chests

Open all N chests in the lexicographically smallest order with single-use typed keys taken from other chests, or report IMPOSSIBLE.

Medium7GreedyGraphBacktrackingNo attempts yetTime limit5sMemory limit512 MB

Problem

An old map led you to the pirate Larry's hidden treasure vault.

The vault holds NN locked chests, and each chest opens only with a key of one particular type. A key disappears once it is used, so no key opens two chests. Every chest holds treasure, and some chests also hold keys to other chests. One chest may hold several keys of the same type, and you may carry any number of keys.

You start with at least one key, and the map records which keys sit inside which chest. Work out an order that opens every chest.

Suppose the vault holds four chests and you start with exactly one key of type 1.

ChestKey type that opens itKey types inside
11none
211, 3
32none
432

Opening the chests in the order 2, 1, 4, 3 opens all four. Opening chest 1 first spends your only key and leaves you stuck.

Input

The first line has the number of test cases TT. TT test cases follow.

Each test case begins with a line holding two positive integers KK and NN: the number of keys you start with and the number of chests you must open.

The next line holds KK integers, the types of the keys you start with.

Then come NN lines. Line ii describes chest ii and begins with two integers tit_i and mim_i: the key type needed to open that chest and the number of keys inside it. Those are followed by mim_i integers, the types of the keys inside the chest.

The limits are as follows.

  • 1T251 \le T \le 25
  • 1K1 \le K
  • 1N2001 \le N \le 200
  • Every key type is an integer between 11 and 200200.
  • One test case holds at most 400 keys in total, counting the starting keys and the keys inside chests.

Output

For each test case, print one line in the form Case #x: C1 C2 ... CN, where xx is the test case number starting from 1 and CiC_i is the index of the ii-th chest you open. Chest indices start from 1.

If several orders open every chest, print the lexicographically smallest one: make C1C_1 as small as possible, then among the orders that do so make C2C_2 as small as possible, and so on.

If no order opens every chest, print Case #x: IMPOSSIBLE on that line instead.