#include "bits/stdc++.h"
#include<unordered_map>
#include<unordered_set>
#pragma warning(disable:4996)
using namespace std;
using ld = long double;
template<class T>
using Table = vector<vector<T>>;
const ld eps=1e-9;

//// < "D:\D_Download\Visual Studio 2015\Projects\programing_contest_c++\Debug\a.txt" > "D:\D_Download\Visual Studio 2015\Projects\programing_contest_c++\Debug\b.answer"
int main() {
	while (1) {
		int M; cin >> M;
		if (!M)break;
		vector<pair<int, int>>cards;
		for (int i = 0; i < M; ++i) {

			int a, b; cin >> a >> b;
			cards.emplace_back(a, b);
		}
		vector<vector<int>>dp(M + 1, vector<int>(10000));
		dp[0][0] = 1;
		for (int i = 0; i < M; ++i) {
			for (int j = 0; j < 9000; ++j) {
				for (int k = 0; k <= cards[i].second; ++k) {
					int plus = dp[i][j];
					for (int l = 0; l < k; ++l) {
						plus *= cards[i].second-l;
					}
					for (int l = 0; l < k; ++l) {
						plus /= l + 1;
					}
					dp[i + 1][j + k*cards[i].first] +=dp[i][j];
				}
			}
		}
		int G; cin >> G;
		for (int i = 0; i < G; ++i) {
			int n; cin >> n;
			cout << dp[M][n] << endl;
		}
	}
	return 0;
}