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

int m, g;
int dp[8][1001];
int a[7], b[7];

int main () {
	while (cin >> m, m) {
		for (int i = 0; i < m; i++) {
			cin >> a[i] >> b[i];
		}
		memset(dp, 0, sizeof(dp));
		dp[0][0] = 1;
		for (int i = 0; i < m; ++i) { // カードの種類
			for (int j = 0; j <= 1000; ++j) { // 点数
				for (int k = 0; k <= b[i]; ++k) { // 枚数
					if (j + a[i] * k > 1000) continue;
					dp[i + 1][j + a[i] * k] += dp[i][j];
				}
			}
		}
		int n;
		cin >> n;
		for (int i = 0; i < n; i++) {
			int x;
			cin >> x;
			cout << dp[m][x] << endl;
		}
	}
	return 0;
}