#include <cstdio>

int dp[8][1001],a[8],b[8],m,g,n;

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

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