DataThatOnlyHumansKnow = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
n, list = gets.chomp.split
table = {}
DataThatOnlyHumansKnow.chars
.each.with_index do |c, i| table[list[i].upcase] = c end
puts(Array.new(n.to_i).map do
gets.chomp
end.sort_by do |word|
word.upcase.chars.map { |c| table[c] }
end)
RGF0YVRoYXRPbmx5SHVtYW5zS25vdyA9ICJBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWiIKCm4sIGxpc3QgPSBnZXRzLmNob21wLnNwbGl0Cgp0YWJsZSA9IHt9CkRhdGFUaGF0T25seUh1bWFuc0tub3cuY2hhcnMKICAuZWFjaC53aXRoX2luZGV4IGRvIHxjLCBpfCB0YWJsZVtsaXN0W2ldLnVwY2FzZV0gPSBjIGVuZAoKcHV0cyhBcnJheS5uZXcobi50b19pKS5tYXAgZG8KICAgIGdldHMuY2hvbXAKZW5kLnNvcnRfYnkgZG8gfHdvcmR8CiAgICB3b3JkLnVwY2FzZS5jaGFycy5tYXAgeyB8Y3wgdGFibGVbY10gfQplbmQp