#include <cstdio>
#include <utility>

using namespace std;

pair<int,int> card[8];
int m;
int dp[8][1005];
int g;

void debug(){
	const int mj = 30;
	for(int i=0;i<=m;i++){
		for(int j=0;j<mj;j++){
			printf("%2d ",dp[i][j]);
		}
		puts("");
	}
}

int main(){
	while(1){
		scanf("%d",&m);
		if(m==0){break;}
		int a,b;
		for(int i=1;i<=m;i++){
			scanf("%d %d",&a,&b);
			card[i] = pair<int,int>(a,b);
		}
		for(int i=0;i<=m;i++){
			for(int j=0;j<=1000;j++){
				if(j == 0){ dp[i][j] = 1; continue;}
				if(i == 0 || j == 0){ dp[i][j] = 0; continue;}
				dp[i][j] = 0;
				dp[i][j] += dp[i-1][j];
				for(int k=1;k<=card[i].second;k++){
					if(j >= card[i].first * k){
						dp[i][j] += dp[i-1][j - card[i].first * k];
					}
				}
			}
		}
		//debug();
		scanf("%d",&g);
		for(int i=0;i<g;i++){
			scanf("%d",&a);
			//printf("dp[%d][%d]=",m,a);
			printf("%d\n",dp[m][a]);
		}
	}
}