while n = gets.to_i
  break if n.zero?

  sum_list = []
  n.times do
    num, m = gets.split.map(&:to_i)
    temp1 = []
    temp2 = []
    m.times do |i|
      temp1 << num * (i+1)
      temp2 << sum_list.map{ |x| x + num * (i+1) }
    end
    sum_list << temp1 << temp2
    sum_list.flatten!
  end

  g = gets.to_i
  g.times do
    p = gets.to_i
    puts sum_list.count(p)
  end
end