// AOJ 0154
#include<iostream>
#include<cstring>

int m;
int a[7], b[7];
int dp[8][1001];

int main()
{
	while( std::cin >> m, m )
	{
		memset( dp, 0, sizeof( dp ) );

		for( int i = 0; i != m; ++i )
			std::cin >> a[i] >> b[i];

		dp[0][0] = 1;
		for( int i = 0; i != m; ++i )
			for( int j = 0; j <= 1000; ++j )
				for( int k = 0; k <= b[i]; ++k )
					if( j + k * a[i] <= 1000 )
						dp[i+1][j+k*a[i]] += dp[i][j];

		int g;
		std::cin >> g;
		while( g-- )
		{
			int n;
			std::cin >> n;
			std::cout << dp[m][n] << std::endl;
		}
	}

	return 0;
}