#include <iostream>
#include <string.h>
using namespace std;

int main()
{
	int m, g;
	int n, dp[8][1001];
	
	cin >> m;

	while (m > 0) {
		memset(dp, 0, sizeof(dp));

		for (int i = 0; i <= m; i++)
			dp[i][0] = 1;

		for (int i = 1; i <= m; i++) {
			int a, b;

			cin >> a >> b;

			for (int j = 1; j < 1001; j++) {
				dp[i][j] = dp[i - 1][j];

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

		cin >> g;

		for (int i = 0; i < g; i++) {
			cin >> n;
			cout << dp[m][n] << endl;
		}

		cin >> m;
	}

	return 0;
}

