#include<bits/stdc++.h>
#define int long long
#define for0(i, n) for(int i = 0; i < (n); i++)
#define for1(i, n) for(int i = 1; i <= (n);i++)
using namespace std;
int m, a[10], b[10], n, g, ans[1010];
void f(int sum, int i) {
	if (sum > 1000)return;
	if (i == m) {
		ans[sum]++; return;
	}
	for0(p, b[i] + 1) f(sum + a[i] * p, i + 1);
}
int get() { int r; cin >> r; return r; }
signed main() {
	while (cin >> m, m) {
		for0(i, m)cin >> a[i] >> b[i];
		for0(i, 1001)ans[i] = 0;
		f(0, 0);
		cin >> g;
		while (g--)cout << ans[get()] << endl;
	}
}
