while True:
try:
a, b = map(int, input().split())
w = ""
for i in range(a):
w += input()
n = list(map(int, input().split()))
#print(n)
#print(w)
for i in n:
print(w[i-1], end="")
print()
x = input()
except:
break