#include<iostream>
using namespace std;

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


int main(){
	for(int j=0; j<1001; j++)
		DP[0][j] = 0;
	DP[0][0] = 1;
	
	while(true){
		cin >> m;
		if(m == 0)
			return 0;
		
		for(int i=1; i<=m; i++){
			cin >> a >> b;
			
			for(int j=0; j<1001; j++)
				DP[i][j] = DP[i-1][j];
			
			for(int j=0; j<1001; j++){
				if(DP[i-1][j] == 0)
					continue;
				for(int k=1; k<=b; k++){
					if(k*a+j > 1000)
						break;
					DP[i][k*a+j] += DP[i-1][j];
				}
			}
			
		}
		
		cin >> g;
		for(int i=0; i<g; i++){
			cin >> n;
			cout << DP[m][n] << endl;
		}
	}
}