#include <bits/stdc++.h>

using namespace std;


int TotalMethodOfCard[8][1001];
int Number[7], Count[7];

int main() {
	int m, g, n;

	while (cin >> m, m) {
		memset(TotalMethodOfCard, 0, sizeof(TotalMethodOfCard));
		memset(Number, 0, sizeof(Number));
		memset(Count, 0, sizeof(Count));

		for (int i = 0; i < m; i++) {
			cin >> Number[i] >> Count[i];
		}

		TotalMethodOfCard[0][0] = 1;
		for (int i = 0; i < m; i++) {
			for (int j = 0; j <= Count[i]; j++) {
				for (int k = 0; k < 10001; k++) {
					if (k + Number[i] * j < 1001) {
						TotalMethodOfCard[i + 1][k + Number[i] * j] += TotalMethodOfCard[i][k];
					}
				}
			}
		}
		cin >> g;
		while (g--) {
			cin >> n;
			cout << TotalMethodOfCard[m][n] << endl;
		}
	}


	return 0;
}
