table = Hash::new
n = gets.to_i()
while(n!=0)
  str=""
  table.clear
  n.times do
    a = gets.split(" ")
    table[a[0].strip] = a[1].strip
  end
  m = gets.to_i()
  m.times do
   c = gets.chomp.strip
   if(table[c]!=nil)
     c = table[c]
   end
   str.concat(c)
  end
  puts str
  n = gets.to_i()
end