import itertools

n, m = [int(elem) for elem in input().split()]
ks = []
ss = []

for i in range(0, m):
  tmp = [int(elem) for elem in input().split()]
  ks.append(tmp[0])
  ss.append(tmp[1:])
  
ps = [int(elem) for elem in input().split()]

res = []
for k, s, p in zip(ks, ss, ps):
  combi = list(itertools.combinations(s, k))
  cand = []
  for elem in combi:
    if p == 0:
      cand.append(list(elem))
    else:
      if len(elem) % p == 0:
        cand.append(list(elem))
  res += cand
  print(res)