#include <iostream>
#include <queue>
#include <set>
#include <vector>
using namespace std;
int as[7];
int bs[7];
int m;
int sums[100 * 10 + 1];
void solve(int n, int sum) {
	if (sum > 1000)
		return;
	if (n == m) {
		sums[sum]++;
		return;
	}
	for (int i = 0; i <= bs[n]; i++) {
		solve(n + 1, sum + (i * as[n]));
	}
}

int main() {
	while (cin >> m, m) {
		memset(sums, 0, sizeof(sums));
		for (int i = 0; i < m; i++) {
			cin >> as[i];
			cin >> bs[i];
		}
		solve(0, 0);
		int g;
		cin >> g;
		for (int i = 0; i < g; i++) {
			int ni;
			cin >> ni;
			cout << sums[ni] << endl;
		}
	}
	return 0;
}