#include <iostream>
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <string.h>
#include <math.h>
#include <climits>
#include <stdlib.h>
#include <map>
#include <set>
#include <queue>
#include <string>
#define MAXA 10000000
#define MOD 1000000007
using namespace std;
typedef set<int> SET;
typedef vector<int> list;
typedef multiset<int> MSET;
typedef int keyType ;
map <int, SET> keyMap;
list ans;
int n;
int debug = 0;
char arr[MAXA],*ptr;
long long ret;
inline long long get_int()
{
ret =0;
while ( !(*ptr >= '0' && *ptr <= '9'))
ptr++;
while (*ptr >= '0' && *ptr<= '9') {
ret = ret*10 + (*ptr - '0');
ptr++;
}
return ret;
}
struct treasure{
int type;
int k;
list key;
int label;
}chests[205],me;
void swapAns(list l){
if(ans.size() == 0)
{
ans = l;
if(debug) cout << "ANS SIZE WAS 0" << endl;
}
else {
for(int xx =0;xx<n;xx++)
{
if(l[xx] < ans[xx])
{
ans = l;
if(debug) cout << "ANSWER SWAPPED at #:" << xx << "ans=" << ans[xx] << " l=" << l[xx] << endl;
return;
}
if(l[xx] > ans[xx])
return;
}
}
}
bool anyUse(list l){
if(ans.size() == 0)
return true;
for(int xx =0;xx<l.size();xx++)
{
if(l[xx] > ans[xx])
return false;
if(l[xx] < ans[xx])
return true;
}
return true;
}
list traverse(list keys, int opened, SET visited, list orderVisited){
vector<int> ::iterator it_v;
vector<int> ::iterator it_v2;
map <int, SET> ::iterator it_m;
multiset <int> ::iterator it_s;
set <int> ::iterator it_ss;
if(debug) cout << endl;
if(debug) cout << "OPENED :" << opened << endl;
if(debug) cout << "KEYS :" ;
if(debug) for(it_v2 = keys.begin();it_v2 != keys.end();it_v2++)
cout << *it_v2 << " ";
if(debug) cout << endl;
if(debug) cout << "ORDERED VISITED CHESTS :" ;
if(debug) for(it_v2 = orderVisited.begin();it_v2 != orderVisited.end();it_v2++)
cout << *it_v2 << " ";
if(debug) cout << endl << endl;
if(opened == n)
return orderVisited;
if(keys.size() == 0)
return orderVisited;
if(!anyUse(orderVisited))
return orderVisited;
for(it_v = keys.begin();it_v != keys.end();it_v++)
{
int key = *it_v;
it_m = keyMap.find(key);
if(it_m != keyMap.end()){
it_s = it_m->second.begin();
while(it_s != it_m->second.end()){
int chest = *it_s;
it_s++;
it_ss = visited.find(chest);
if( it_ss == visited.end())
{
if(debug) cout << "KEY USED :" << key << " TO VISIT chest #" << chest << endl;
bool flag = false;
list tlist2 = orderVisited;
list tlist;
for(it_v2 = keys.begin();it_v2 != keys.end();it_v2++)
{
if(*it_v2 != key || flag)
{
tlist.push_back(*it_v2);
}
if(*it_v2 == key)
flag = true;
}
for(int xx =0;xx<chests[chest].k;xx++)
tlist.push_back(chests[chest].key[xx]);
if(debug) cout << "KEYS WITH ME BEFORE I GO :";
if(debug) for(it_v2 = tlist.begin();it_v2 != tlist.end();it_v2++)
cout << *it_v2 << " ";
if(debug) cout << endl;
SET tset = visited;
tset.insert(chest);
tlist2.push_back(chest);
tlist = traverse(tlist, opened+1, tset, tlist2 );
if(tlist.size() == n)
swapAns(tlist);
}
}
}
}
return orderVisited;
}
int main()
{
map <int, SET> ::iterator it;
fread(arr,sizeof(char),MAXA,stdin);
ptr = arr;
int t = get_int();
if(debug) cout << "T: " << t << endl;
int k;
for(int tt = 1; tt <= t; tt++)
{
ans.clear();
keyMap.clear();
me.k = get_int();
n = get_int();
me.key.clear();
for(int xx =0;xx<n;xx++)
chests[xx].key.clear();
if(debug) cout << "MY Key Count : " << me.k << " N: " << n << endl;
for(int xx =0;xx<me.k;xx++)
me.key.push_back(get_int());
for(int xx =0;xx<n;xx++)
{
chests[xx].type = get_int();
chests[xx].k = get_int();
chests[xx].label = 0;
if(debug) cout << "TYPE :" << chests[xx].type << " KEY COUNT :" << chests[xx].k << endl;
for(int yy =0;yy<chests[xx].k;yy++)
chests[xx].key.push_back(get_int());
if(debug) cout << "KEYS :";
if(debug) for(int yy =0;yy<chests[xx].k;yy++)
cout << chests[xx].key[yy] << " ";
if(debug) cout << endl;
it = keyMap.find(chests[xx].type);
if(it == keyMap.end())
{
SET temp;
temp.insert(xx);
keyMap[chests[xx].type] = temp;
}
else
(it->second).insert(xx);
}
SET visited;
list tempo;
if(debug) cout << "\n";
tempo = traverse(me.key, 0, visited, tempo);
cout << "Case #" << tt << ": ";
if(ans.size() == n)
{
for(int xx =0;xx<n;xx++)
cout << ans[xx]+1 << " ";
cout << endl;
}
else
cout << "IMPOSSIBLE" << endl;
}
return 0;
}