#include <iostream>
using namespace std;

int card_value[7];
int card_num[7];
int sum1[1001];
int sum2[1001];
int sum3[1001];

void get_sum(int a, int b, int sum[], int count){
	if(a >= b){
		if(count <= 1000){
			sum[count]++;
		}
		
	}else{
		for(int i = 0; i <= card_num[a]; i++){
			get_sum(a+1, b, sum, count + card_value[a] * i);
		}
	}
}
	

int main(){
	int g;
	int m;
	while(cin >> m){
		if(m == 0) break;
		for(int i = 0;i < m; i++){
			cin >> card_value[i] >> card_num[i];
		}
		for(int i = 0; i < 1001; i++){
			sum1[i] = 0;
			sum2[i] = 0;
			sum3[i] = 0;
		}
		get_sum(0, m / 2, sum1, 0);
		get_sum(m / 2, m, sum2, 0);
		for(int i = 0; i < 1001; i++){
			for(int j = 0; j < 1001 - i; j++){
				sum3[i+j] += sum1[i]*sum2[j];
			}
		}
		cin >> g;
		for(int i = 0; i < g; i++){
			int q;
			cin >> q;
			cout << sum3[q] << endl;
		}
	}
	return 0;
}