N,M = map(int, input().split())
S = [[]for i in range(M)]
for i in range(M):
    k_s = list(map(int, input().split()))
    for j in range(len(k_s)-1):
        S[i].append(k_s[j+1])
p = list(map(int, input().split()))