#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

typedef pair<int, int> P;

int main() {
	int m;
	while (cin >> m) {
		if (m == 0) {
			break;
		}
		vector<int> dp(7010, 0);
		vector<int> dp2(7010, 0);
		vector<P> data(m);
		for (int i = 0; i < m; i++) {
			cin >> data[i].first >> data[i].second;
		}
		for (int i = 0; i < m; i++) {
			vector<int> d;
			for (int j = 0; j < 7010; j++) {
				if (dp[j] > 0) {
					d.push_back(j);
				}
			}
			for (int j = 0; j < data[i].second; j++) {
				dp2[(data[i].first)*(j+1)]++;
			}
			for (int j = 0; j < d.size(); j++) {
				for (int k = 0; k < data[i].second; k++) {
					dp2[d[j]+(data[i].first)*(k+1)] += dp[d[j]];
				}
			}
			for (int j = 0; j < 7010; j++) {
				dp[j] = dp2[j];
			}
		}
		int g;
		cin >> g;
		int n;
		for (int i = 0; i < g; i++) {
			cin >> n;
			cout << dp[n] << endl;
		}
	}
	return 0;
}