#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
//#define int long long
using namespace std;

#define INF 999999

int m;
int num[7];
int maisu[7];
int g;
int dp[8][1001];
int n;

signed main()
{
	while (true)
	{
		cin >> m;
		if (m == 0) break;

		rep(i,8) rep(j,1001) dp[i][j] = 0;

		rep(i,m)
		{
			cin >> num[i] >> maisu[i];
		}

		dp[0][0] = 1;
		/*
		for (int i = num[0]; i <= num[0] * maisu[0]; i += num[0])
		{
			dp[0][i] = 1;
		}
		*/

		rep(i, m)
		{
			rep(j,1001)
			{
				rep(k,maisu[i] + 1)
				{
					if (j + num[i] * k <= 1000)
					{
						dp[i + 1][j + num[i] * k] += dp[i][j];
					}
				}
			}
		}

		cin >> g;

		rep(i,g)
		{
			cin >> n;
			printf("%d\n", dp[m][n]);
		}
	}
}