#!/usr/bin/env python3
line = input().split(" ")
n = int(line[0])
k = int(line[1])

d = {}
list_of_lists =[]
items_bought = input().split(" ")
distinct_categories = input().split(" ")

for i in range(0,k):
    list_of_lists.append([])
    category_attributes = input().split(" ")
    category_name = category_attributes[0]
    number_of_stuff_in_category = int(category_attributes[1])
    d[category_name] = i
    for j in range(0,number_of_stuff_in_category):
        item = category_attributes[j+2]
        d[item] = category_name

#Legg items i riktig liste
for i in range(0,n):
    cat = d[items_bought[i]] #Hvilken kategori er det?
    pos = d[cat] #Hva er posisjonen til kategorien?
    list_of_lists[pos].append(items_bought[i]) #Legg til produktet på riktig plass i sorteringen


#Print each sublist in correct order
for i in range(0,k):
    pos = d[distinct_categories[i]]

    for j in list_of_lists[pos]:
        print(j, end= " ")
