while true
  n = gets.chomp.to_i
  break if n == 0
  trans = {}
  n.times do |i|
    x, y = gets.chomp.split(" ")
    trans[x] = y
  end
  m = gets.chomp.to_i
  result = ""
  m.times do |i|
    x = gets.chomp
    result << (trans[x] || x)
  end
  puts result
end