#include<map>
#include<set>
#include<list>
#include<cmath>
#include<queue>
#include<stack>
#include<cstdio>
#include<string>
#include<vector>
#include<complex>
#include<cstdlib>
#include<cstring>
#include<numeric>
#include<sstream>
#include<iostream>
#include<algorithm>
#include<functional>

#define mp       make_pair
#define pb       push_back
#define all(x)   (x).begin(),(x).end()
#define rep(i,n) for(int i=0;i<(n);i++)
#define REP(i,b,n) for(int i=b;i<n;i++)

using namespace std;

typedef    long long          ll;
typedef    unsigned long long ull;
typedef    vector<bool>       vb;
typedef    vector<int>        vi;
typedef    vector<vb>         vvb;
typedef    vector<vi>         vvi;
typedef    pair<int,int>      pii;

const int INF=1<<29;
const double EPS=1e-9;

const int dx[]={1,0,-1,0},dy[]={0,-1,0,1};
int m,a,b;
int g,n;
pii card[110];
int dp[100][1010];
int main(){
	while(cin>>m,m){
		memset(card,0,sizeof(card));
		memset(dp,0,sizeof(dp));
		rep(i,m){
			cin>>a>>b;
			card[i].first=a;
			card[i].second = b;
		}
		cin>>g;
		for(int j =0;j <=card[0].second&&card[0].first*j<=1000;j++){
			dp[0][card[0].first*j]++;
		}
		int j;
		for(int i =1;i <m;i++){
			j =card[i].first;
			for(int k =0;k <=card[i].second;k++){
				for(int l =0;l+j*k<=1000;l++){
					dp[i][l+j*k]+=dp[i-1][l];
					//cout <<dp[i][l+j*k]<<endl;
				}
			}
		}

		rep(i,g){
			cin>>n;
			cout <<dp[m-1][n]<<endl;
		}
	}
	return 0;
}