#include <vector>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <cstring>
#include <deque>
#include <cstdio>
#include <cmath>
#include <complex>
#include <iostream>
#include <iterator>

using namespace std;

#define reep(i,f,t) for(int i=f ; i<int(t) ; ++i)
#define rep(i,n) reep(i, 0, n) 

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;

int m;
vector<pii> card;
int memo[8192];
void search(int sum, int k)
{
	if(k == m)
		memo[sum]++;
	else{
		rep(i, card[k].second+1)
			search(sum + card[k].first * i, k+1);
	}
}

int main()
{
	while(scanf("%d", &m), m){
		card.resize(m);
		rep(i, m)
			scanf("%d%d", &card[i].first, &card[i].second);
		
		fill_n(memo, 8192, 0);
		search(0, 0);
		
		int g;
		scanf("%d", &g);
		rep(i, g){
			int n;
			scanf("%d", &n);
			printf("%d\n", memo[n]);
		}
	}
	
	return 0;
}