while true
  n = gets.to_i
  exit if n == 0
  table = {}
  for i in 1..n do
    key, value = gets.split
    table[key] = value
  end
  m = gets.to_i
  for i in 1..m do
    key = gets.strip
    print (table[key] or key)
  end
  puts
end