while (n = gets.to_i) != 0 do
  conv = {}
  n.times do
    s = gets.split
    conv[s[0]] = s[1]
  end

  result = ""
  gets.to_i.times do
    s = gets.chomp
    if conv.has_key?(s)
      result += conv[s]
    else
      result += s
    end
  end
  puts result
end