#include <iostream>
#include <cstring>	// require memset
//#include <fstream>	// require freopen

using namespace std;
int memo[1001],a[7],b[7];

void comb (int c, int s, int m)
{

	if (s > 1000 ){
		return;
	} // end if
	if (c == m ){
		memo[s]++; return;
	} // end if
	
	for (int i = 0; i <= b[c]; ++i ){
		comb (c+1, s+a[c]*i, m );
	} // end for
}	

int main()
{
//	cut here before submit 
//	freopen ("testcase.sum_of_card", "r", stdin );
	int m;

	while (cin >> m && m ){
		for (int i = 0; i < m; ++i ){
			cin >> a[i] >> b[i];
		} // end for
		memset (memo, 0, sizeof(memo ) );
		comb (0, 0, m);
		int g;
		cin >> g;
		for (int i = 0; i < g; ++i ){
			int ng;
			cin >> ng;
			int res = memo[ng];
			cout << res << endl;
		} // end for
	} // end loop
		
	return 0;
}