#include <cstdio>
using namespace std;

int dp[1001];

int main(){
	int m, a, b, g, n;
	while(scanf("%d", &m), m){
		dp[0] = 1;
		for(int i = 1; i <= 1000; ++i){
			dp[i] = 0;
		}

		for(int i = 0; i < m; ++i){
			scanf("%d%d", &a, &b);
			for(int j = 1000; j >= a; --j){
				for(int k = 1; k <= b && j - k * a >= 0; ++k){
					dp[j] += dp[j - k * a];
				}
			}
		}

		scanf("%d", &g);
		for(int i = 0; i < g; ++i){
			scanf("%d", &n);
			printf("%d\n", dp[n]);
		}
	}
}