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

int n,m;

void solve()
{
	int dp[11][15001] = {};
	int num,how;
	int res = 0;

	for(int i = 0; i < n; i++){
		cin >> num >> how;
		
		for(int j = 1; j <= how; j++){
			dp[i+1][num*j]++;
		}
		for(int j = 1; j <= 10000; j++){
			if(dp[i][j]){
				dp[i+1][j] += dp[i][j];
				for(int k = 1; k <= how && j+num*k < 10000; k++){
					dp[i+1][j+num*k] += dp[i][j];
				}
			}
		}
	}
	cin >> m;
	for(int i = 0; i < m; i++){
		cin >> num;
		cout << dp[n][num] << endl;
	}

	
	
}

int main(void)
{
	while(cin >> n, n){
		solve();
	}
	return 0;
}