loop do
  n = gets.to_i
  arr = []
  convert_list = {}

  break if n.zero?

  n.times do
    from, to = gets.split(' ')

    convert_list[from] = to
  end

  m = gets.to_i
  m.times do
    str = gets.chomp

    str = convert_list[str] unless convert_list[str].nil?

    arr << str
  end

  puts arr.join
end