words = input().split('\n')
num, alpha = words.pop(0).split(' ')
alpha_dict = {letter.upper():index for index,letter in enumerate(alpha)}
words.sort(key = lambda word: [alpha_dict[letter.upper()] for letter in word.upper()])
for word in words:
print(word)
d29yZHMgPSBpbnB1dCgpLnNwbGl0KCdcbicpCm51bSwgYWxwaGEgPSB3b3Jkcy5wb3AoMCkuc3BsaXQoJyAnKQphbHBoYV9kaWN0ID0ge2xldHRlci51cHBlcigpOmluZGV4IGZvciBpbmRleCxsZXR0ZXIgaW4gZW51bWVyYXRlKGFscGhhKX0Kd29yZHMuc29ydChrZXkgPSBsYW1iZGEgd29yZDogW2FscGhhX2RpY3RbbGV0dGVyLnVwcGVyKCldIGZvciBsZXR0ZXIgaW4gd29yZC51cHBlcigpXSkKZm9yIHdvcmQgaW4gd29yZHM6CiAgICBwcmludCh3b3JkKQ==