module Memoizable
	def memoize(name)
		original = "__original_#{name}__"
		([Class, Module].include?(self.class) ? self : self.class).class_eval {
			alias_method(original, name)
			private(original)
			cache = {}
			define_method(name){|*args|
				cache[args] ||= send(original, *args)
			}
		}
	end 
end
include Memoizable

def f(arr, i, s)
    return 0 if s < 0
    return 1 if s == 0
    return 0 if i < 0
    a, b = arr[i]
    sum = (0..b).map {|k| f(arr, i-1, s - a * k)}.inject(:+)
end

memoize :f

loop do
    n = gets.to_i
    break if n == 0
    arr = (1..n).map { gets.split.map(&:to_i) }
    g = gets.to_i
    g.times do
        p f(arr, arr.size - 1, gets.to_i)
    end
end

