count, quiz = map(int,input().split())
girls = {}
for i in range(0,count):
group_name = input()
num = int(input())
g_list = []
for j in range(0,num):
g_list.append(input())
g_list.sort()
girls[group_name] = g_list
q = []
for i in range(0,quiz):
temp = input()
temp += ":"
temp += input()
q.append(temp)
for i in q:
qq = i.split(':')
if qq[1]=='0':
for i in girls.get(qq[0]):
print(i)
else:
for j in girls:
if qq[0] in girls[j]:
print(j)
N, M = map(int, input().split())
team_mem, mem_team = {}, {}
for i in range(N):
team_name, mem_num = input(), int(input())
team_mem[team_name] = []
for j in range(mem_num):
name = input()
team_mem[team_name].append(name)
mem_team[name] = team_name
for i in range(M):
name, q = input(), int(input())
if q:
print(mem_team[name]:
else:
for mem in sorted(team_mem[name]):
print(mem)