#include<iostream>
#include<map>
using namespace std;

int m,M,*w,*v,*c,g,*n,cnt;

void solve(int i, int n){
	if(n==0){
		cnt++;
		return;
	}
	if(i == M)
		return;
	else if(c[i] > n){
		solve(i+1, n);
	}else{
		solve(i+1, n-c[i]);
		int x;
		for(x=1; i+x<M; x++)
			if(c[i+x] != c[i])
				break;
		solve(i+x, n);
	}
}

int main(){
	while(true){
		M = 0;
		cin >> m;
		if(!m)
			return 0;
		w = new int[m];
		v = new int[m];
		for(int i=0; i<m; i++){
			cin >> w[i] >> v[i];
			M += v[i];
		}
		c = new int[M];
		cnt = 0;
		for(int i=0; i<m; i++){
			for(int k=1; k<=v[i]; k++){
				c[cnt] = w[i];
				cnt++;
			}
		}
		cnt = 0;
		cin >> g;
		n = new int[g];
		for(int i=0; i<g; i++)
			cin >> n[i];
		for(int i=0; i<g; i++){
			cnt = 0;
			solve(0, n[i]);
			cout << cnt << endl;
		}
		delete[] w;
		delete[] v;
		delete[] c;
	}
}