loop do
  n = gets.to_i
  exit if n == 0

  convmap = Hash.new
  n.times do
    conv = gets.chomp.split(" ")
    convmap[conv[0]] = conv[1]
  end

  src = []
  m = gets.to_i
  m.times do
    src << gets.chomp
  end

  res = src.map{ |val|
    convmap.has_key?(val) ? convmap[val] : val
  }.join("")

  puts res

end