word_hash = Hash.new([])
output = Array.new()
while n = gets
  n = n.to_i
  break if n == 0
  n.times do
  	before, after = gets.chomp.split
  	word_hash.store(before, after)
  end
  m = gets.to_i
  m.times do
  	word = gets.chomp
  	word_hash.each{|key, value|
  	  if word == key
  	  	word = value
  	  	break
  	  end
    }
    output << word
  end
  
  puts output.join
end