#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>

#define REP(i,k,n) for(int i=k;i<n;i++)
#define rep(i,n) for(int i=0;i<n;i++)

using namespace std;

int cnt;
vector<pair<int,int> > card;

int main()
{
	int n;
	int dp[8][1001];
	while(cin >> n && n)
	{
		card.clear();
		int a,b;

		rep(i,n)
		{
			cin >> a >> b;
			card.push_back(make_pair(a,b));
		}
		
		memset(dp,0,sizeof(dp));
		dp[0][0] = 1;
		
		rep(i,n)
		{
			rep(j,1001)
			{
				rep(k,card[i].second+1)
				{
					if(j + card[i].first*k <= 1000) dp[i+1][j+card[i].first*k] += dp[i][j];
				}
			}
		}

		int g;
		cin >> g;

		rep(i,g)
		{
			int n2;
			cin >> n2;
			
			cout << dp[n][n2] << endl;
		}
	}
	return 0;
}