#include <stdio.h>
#include <cmath>
#include <algorithm>


void func(int m){
	int dp[1001][m+1],num,count,div;

	for(int i = 0; i <= 1000; i++){
		for(int k = 0; k <= m; k++)dp[i][k] = 0;
	}

	for(int i = m-1; i >= 0; i--){
		scanf("%d %d",&num,&count);

		for(int k = 1; k <= count;k++){
			div = num*k;
			if(dp[div][i] == 0){
				dp[div][i] = dp[div][i+1]+1;
			}else{
				dp[div][i]++;
			}

			for(int p = div+1; p <= 1000; p++){
				if(dp[p-div][i+1] > 0){
					if(dp[p][i] == 0){
						dp[p][i] = dp[p][i+1]+dp[p-div][i+1];
					}else{
						dp[p][i] += dp[p-div][i+1];
					}
				}
			}
		}
		for(int k = 1; k <= 1000; k++){
			if(dp[k][i] == 0 && dp[k][i+1] > 0)dp[k][i] = dp[k][i+1];
		}
	}

	int game_count,to_make;
	scanf("%d",&game_count);

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

int main(){

	int m;

	while(true){
		scanf("%d",&m);
		if(m == 0)break;

		func(m);
	}


	return 0;
}