#!/usr/bin/env python3

n, k = map(int, input().split())
items = list(input().split())
order = list(input().split())

index_of_category = {}
for i, cat in enumerate(order): index_of_category[cat] = i

category_of_item = {}
for _ in range(k):
    line = input().split()
    cat = line[0]
    for item in line[2:]:
        category_of_item[item] = cat

items.sort(key=lambda x: index_of_category[category_of_item[x]])
print(" ".join(items))
