#include <vector>
#include <iostream>

using namespace std;

int main()
{
	int N, G, A, B;

	while (true)
	{
		cin >> N;

		if (N == 0) { break; }

		vector<vector<int> > dp(N + 1, vector<int>(1001, 0));

		dp[0][0] = 1;

		for (int i = 1; i <= N; i++)
		{
			cin >> A >> B;

			for (int j = 0; j <= 1000; j++)
			{
				for (int k = 0; k <= A * B; k += A)
				{
					if (j - k >= 0)
					{
 						dp[i][j] += dp[i - 1][j - k];
					}
				}
			}
		}

		cin >> G;

		for (int i = 0; i < G; i++)
		{
			cin >> A;

			cout << dp[N][A] << endl;
		}
	}

	return 0;
}