#include <iostream>
using namespace std;
int a[7][2],c;
int f(int tc,int tp,int maxc,int maxp){
	if(tc == maxc) {
		if(maxp == tp) ++c;
	}
	else {
		for(int i = 0;i <= a[tc][1];++i){
			f(tc + 1,tp + (a[tc][0] * i),maxc,maxp);
		}
	}
	return 0;
}
int main(){
	int n,b,price;
	while(cin >> n,n){
		for(int i = 0;i < n;++i){
			cin >> a[i][0] >> a[i][1];
		}
		cin >> b;
		for(int i = 0;i < b;++i){
			c = 0;
			cin >> price;
			f(0,0,n,price);
			cout << c << endl;
		}
	}
	return 0;
}