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{
      s += [gets[0]]
    }
    for x in 0...s.length
      for y in 0...c.length
	if s[x] == c[y][0] then
	  s[x] = c[y][1]
	  break
	end
      end
    end
    puts s
  end
end