def main(input = STDIN)
  code = Hash.new{|hash, key|key}
  input.gets.to_i.times do
    code.store(*input.gets.chomp.split(" "))
  end
  str = ""
  input.gets.to_i.times do
    str << code[input.gets.chomp]
  end
  puts str
end
main