while (n1 = gets.to_i) != 0
  hash = Hash.new
  n1.times do
    c1, c2 = gets.split(" ")
    hash[c1] = c2
  end 
  n2 = gets.to_i
  n2.times do
    char = gets.chomp!
    char = hash[char] if hash[char]
    print char
  end 
  print "\n"
end