#include <iostream>
using namespace std;
#define rep2(x,from,to) for(int x=(from);x<(to);++(x))
#define rep(x,to) rep2(x,0,to)
int main() {
	int m, g, s;
	while(cin >> m, m) {
		int a, b, dp[1010][7] = {};
		rep(i,m) {
			cin >> a >> b;
			rep2(j,1,b+1) {
				dp[a*j][i] = 1;
			}
			if(i!=0) {
				rep2(k,1,1001) {
					if(dp[k][i-1] != 0) {
						rep(l,b+1) {
							if(k+a*l <= 1000) dp[k+a*l][i] += dp[k][i-1];
						}
					}
				}
			}
		}
		cin >> g;
		rep(i,g) {
			cin >> s;
			cout << dp[s][m-1] << endl;
		}
	}
	return 0;
}