#include<bits/stdc++.h>
#define rep(i,n)for(int i=0;i<n;i++)
using namespace std;

int dp[1001];
int main() {
	int m;
	while (scanf("%d", &m), m) {
		memset(dp, 0, sizeof(dp));
		dp[0] = 1;
		rep(i, m) {
			int a, b; scanf("%d%d", &a, &b);
			for (int j = 1000; j >= 0; j--) {
				for (int k = 1; k <= b; k++) {
					if (j >= a*k)dp[j] += dp[j - a*k];
				}
			}
		}
		int g; scanf("%d", &g);
		rep(i, g) {
			int n; scanf("%d", &n);
			printf("%d\n", dp[n]);
		}
	}
}