def main(input = STDIN)
  while 0 != n = input.gets.to_i
    code = Hash.new{|hash, key|key}
    n.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
end
main