#include <iostream>
#include <vector>
#include <cstring>
using namespace std;

const int MEMO_MAX = 7000;

int main()
{
	int memo[ MEMO_MAX ];
	int m;

	while( cin >> m && m ){
		memset( memo, 0, sizeof(memo) );
		vector< pair< int, int > > cardList;
		for( int i = 0; i < m; ++i ){
			int figure, pieces;
			cin >> figure >> pieces;
			cardList.push_back( make_pair( figure, pieces ) );
		}
		int sumMax = 0;
		for( int i = 0; i < m; ++i ){
			int number = cardList[ i ].first;
			int pieces = cardList[ i ].second;
			int high = sumMax;
			int tmpMemo[ 7000 ];
			memset( tmpMemo, 0, sizeof(tmpMemo) );
			for( int j = 1; j <= pieces; ++j ){
				int sumMono = number * j;
				for( int k = 1; k <= high; ++k ){
					int sum = k + sumMono;
					tmpMemo[ sum ] += memo[ k ];
					if( sumMax < sum ){
						sumMax = sum;
					}
				}
				++tmpMemo[ sumMono ];
				if( sumMax < sumMono ){
					sumMax = sumMono;
				}
			}
			for( int j = 1; j <= sumMax; ++j ){
				memo[ j ] += tmpMemo[ j ];
			}
		}

		int g;
		cin >> g;
		for( int i = 0; i < g; ++i ){
			int d;
			cin >> d;
			cout << memo[ d ] << endl;
		}
	}

	return 0;
}