#include<iostream>
#include<cstring>
using namespace std;

#define MAX_N 150000
#define MAX_CARD 120

long long dp[MAX_CARD][MAX_N];
long long card[MAX_CARD][2];
int n, m, a, b, cnt, sum;

int main() {
	while (true) {
		memset(dp, 0, sizeof(dp));
		memset(card, 0, sizeof(card));
		//cin.
		cin >> n;
		if (n == 0) {
			break;
		}
		cnt = 0;
		sum = 0;
		for (int i = 0; i < n; i++) {
			cin >> a >> b;
			card[i][0] = a;
			card[i][1] = b;
			for (int j = 0; j < b; j++) {
				sum += a;
			}
		}
		//dp.
		dp[0][0] = 1;
		for (int i = 1; i <= n; i++) {
			for (int j = 0; j <= sum; j++) {
				if (dp[i - 1][j] != 0) {
					for (int k = 0; k <= card[i - 1][1]; k++) {
						dp[i][j + k*card[i - 1][0]] += dp[i - 1][j];
					}
				}
			}
		}
		//cin2.
		cin >> m;
		for (int i = 0; i < m; i++) {
			cin >> a;
			cout << dp[n][a] << endl;
		}
	}
	return 0;
}