#include <iostream>
using namespace std;

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