Under construction

Still being built. Things may change

Treasure Chests (Small)

Time limit5sMemory limit512 MB

Summary
Open all N chests in the lexicographically smallest valid order using keys found inside chests, or report IMPOSSIBLE.
Level

Medium6 of 10

Topics
Backtracking, DFS, Graph, Greedy
Solved
No attempts yet

Problem

You followed an old map and found the secret treasure trove of Larry the pirate.

The trove holds NN locked chests. Each chest opens with one key of a specific type, and a key that opens a chest is gone and cannot be used again. Every chest holds treasure, and it may also hold keys that open other chests. A 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. Use that information to find an order that opens every chest.

For example, suppose there are four chests as listed below and you start with a single key of type 1.

Chest numberKey type that opens the chestKey types inside the chest
11none
211, 3
32none
432

Opening them in the order 2, 1, 4, 3 opens all four chests. If you open chest 1 first, you spend your only key and no chest can be opened after that.

Input

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

The first line of each test case contains two positive integers KK and NN, the number of keys you start with and the number of chests you must open.

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

Each of the next NN lines describes one chest. Line ii begins with two integers TiT_i and KiK_i, the key type needed to open chest ii and the number of keys inside that chest. Then come KiK_i integers, the types of the keys inside that chest.

Limits

  • 1≤T≤251 \le T \le 25
  • 1≤K1 \le K
  • Every key type is an integer between 11 and 200200.
  • 1≤N≤201 \le N \le 20
  • In each test case, the keys you start with and the keys inside the chests add up to at most 400 keys.

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 number of the iith chest you open. Chest numbers start from 1.

If several orders open all the chests, print the lexicographically smallest one. That is, make C1C_1 as small as possible, and among the orders that do so make C2C_2 as small as possible, and continue the same way for the later positions.

If no order opens all the chests, print Case #x: IMPOSSIBLE for that test case.

Examples1

  1. Example 1

    Input
    3
    1 4
    1
    1 0
    1 2 1 3
    2 0
    3 1 2
    3 3
    1 1 1
    1 0
    1 0
    1 0
    1 1
    2
    1 1 1
    
    Expected output
    Case #1: 2 1 4 3
    Case #2: 1 2 3
    Case #3: IMPOSSIBLE