#include <bits/stdc++.h>
using namespace std;

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) FOR(i,0,n)

int dp[7][7001];
int card[7], cnt[7];
int N, M;

int dfs(int pos, int now) {
	if (pos == -1) return now == 0;
	if (now < 0) return 0;
	
	if (dp[pos][now] != -1) return dp[pos][now];
	
	dp[pos][now] = 0;
	REP(i, cnt[pos] + 1) dp[pos][now] += dfs(pos - 1, now - card[pos] * i);
	
	return dp[pos][now];
}

int main() {
	while (cin >> M, M) {
		REP(i, M) scanf("%d %d", &card[i], &cnt[i]);
		cin >> N;
		
		fill(dp[0], dp[7], -1);
		REP(i, N) {
			int n;
			scanf("%d", &n);
			cout << dfs(M - 1, n) << endl;
		}
	}
	return 0;
}