#include <iostream>
#include <vector>

using namespace std;

void solve()
{
	int m;
	while(cin >> m, m)
	{
		vector<int> a(m);
		vector<int> b(m);
		for(int i = 0; i < m; ++i)
		{
			cin >> a[i] >> b[i];
		}
		int dp[8][1001] = {0};
		dp[0][0] = 1;
		for(int i = 0; i < m; ++i)
		{
			for(int j = 0; j < 1001; ++j)
			{
				for(int k = 0; k <= b[i]; ++k)
				{
					if(j + k * a[i] > 1000)
					{
						break;
					}
					dp[i + 1][j + k * a[i]] += dp[i][j];
				}
			}
		}
		int g;
		cin >> g;
		for(int i = 0; i < g; ++i)
		{
			int declare;
			cin >> declare;
			cout << dp[m][declare] << endl;
		}
	}
}

int main()
{
	solve();
	return(0);
}