until (n = gets.chomp.to_i) == 0 ? true:false
h = Hash.new
n.times do
  line = gets.split{" "}
  h[line[0]] = line[1]
end
n = gets.chomp.to_i
n.times do
  word = gets.chomp
  print ((h[word] == nil) ? word : h[word])
end
print "\n"
end