import sys



def main():

	while True:

		m = eval(input())

		if m == 0: return 0

		dp = {}

		dp[(0, 0)] = 1

		for i in range(m):

			v, c = list(map(int, input().split()))

			for j in range(1001):

				for k in range(c + 1):

					key = (i + 1, j + v * k)

					dp.setdefault(key, 0)

					dp[key] += dp.get((i, j), 0)

		n = eval(input())

		for i in range(n):

			x = eval(input())

			print(dp.get((m, x), 0))



if __name__ == '__main__':

	sys.exit(main())
import sys

from array import array



def main():

	while True:

		m = eval(input())

		if m == 0: return 0

		dp = [array('I', [0] * 1001) for i in range(m + 1)]

		dp[0][0] = 1

		for i in range(m):

			v, c = list(map(int, input().split()))

			for j in range(1001):

				for k in range(c + 1):

					next = j + v * k

					if next > 1000: continue

					dp[i + 1][next] += dp[i][j]

		n = eval(input())

		for i in range(n):

			x = eval(input())

			print(dp[m][x])



if __name__ == '__main__':

	sys.exit(main())
import sys


def main():
    while True:
        m = eval(input())
        if m == 0:
            return 0
        dp = {}
        dp[(0, 0)] = 1
        for i in range(m):
            v, c = list(map(int, input().split()))
            for j in range(1001):
                for k in range(c + 1):
                    key = (i + 1, j + v * k)
                    dp.setdefault(key, 0)
                    dp[key] += dp.get((i, j), 0)
        n = eval(input())
        for i in range(n):
            x = eval(input())
            print(dp.get((m, x), 0))


if __name__ == "__main__":
    sys.exit(main())

import sys
from array import array


def main():
    while True:
        m = eval(input())
        if m == 0:
            return 0
        dp = [array("I", [0] * 1001) for i in range(m + 1)]
        dp[0][0] = 1
        for i in range(m):
            v, c = list(map(int, input().split()))
            for j in range(1001):
                for k in range(c + 1):
                    next = j + v * k
                    if next > 1000:
                        continue
                    dp[i + 1][next] += dp[i][j]
        n = eval(input())
        for i in range(n):
            x = eval(input())
            print(dp[m][x])


if __name__ == "__main__":
    sys.exit(main())

+from array import array -        dp = {} -        dp[(0, 0)] = 1 +        dp = [array("I", [0] * 1001) for i in range(m + 1)] +        dp[0][0] = 1 -                    key = (i + 1, j + v * k) -                    dp.setdefault(key, 0) -                    dp[key] += dp.get((i, j), 0) +                    next = j + v * k +                    if next > 1000: +                        continue +                    dp[i + 1][next] += dp[i][j] -            print(dp.get((m, x), 0)) +            print(dp[m][x])