#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<cassert>
#include<iostream>
#include<sstream>
#include<string>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<utility>
#include<numeric>
#include<algorithm>
#include<bitset>
#include<complex>

using namespace std;

typedef long long Int;
typedef vector<int> vint;
typedef pair<int,int> pint;
#define mp make_pair

template<class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cout << *i << " "; cout << endl; }
template<class T> void chmin(T &t, T f) { if (t > f) t = f; }
template<class T> void chmax(T &t, T f) { if (t < f) t = f; }
int in() { int x; scanf("%d", &x); return x; }

int res[10][2000];

int main() {
	int n;
	while(n=in()){
		int i;
		int a,b;
		memset(res,0,sizeof(res));
		res[0][0]=1;
		for(i=0;i<n;i++){
			cin>>a>>b;
//cout<<"piyo"<<endl;
			int j,k;
			for(j=0;j<=1000;j++){
//cout<<i<<" "<<j<<endl;
				for(k=0;k<=b;k++){
					if(0>j-a*k)break;
					res[i+1][j]+=res[i][j-a*k];
				}
			}
		}
//cout<<"hoge"<<endl;
		int m=in();
		for(i=0;i<m;i++){
			int j;
			cin>>j;
			cout<<res[n][j]<<endl;
		}
	}
	return 0;
}