while true
  n = gets.to_i
  if n == 0 then
    break
  else
    c = []
    s = []
    n.to_i.times{
      c += [gets.split]
    }
    gets.to_i.times{
      ss = gets[0]
      for y in c
	if ss == y[0] then
	  ss = y[1]
	  break
	end
      end
      s += [ss]
    }
    puts s
  end
end