#include <iostream>
using namespace std;

int main(){
	int m,a[7],b[7],g,n;
	while(cin >> m,m){
		for(int i = 0;i < m;i++) cin >> a[i] >> b[i];
		int dp[8][1001] = {};
		dp[0][0] = 1;
		for(int i = 1;i <= m;i++){
			for(int j = 0;j <= 1000;j++){
				for(int k = 0;k <= b[i - 1];k++){
					if(j - k * a[i - 1] < 0) break;
					dp[i][j] += dp[i - 1][j - k * a[i - 1]];
				}
			}
		}
		cin >> g;
		for(int i = 0;i < g;i++){
			cin >> n;
			cout << dp[m][n] << endl;
		}
	}
	return 0;
}