#include<iostream>
#include<cstdio>
#include<algorithm>
#include<climits>
#include<string>
#include<vector>
#include<list>
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<cstring>
#include<stack>
#include<functional>
using namespace std;

const int dy[8] = {-1,-1,0,1,1,1,0,-1}, dx[8] = {0,1,1,1,0,-1,-1,-1};

int main(){
	int M;
	while(cin>>M,M){
		int dp[1001] = {}; dp[0] = 1;
		for(int k=0;k<M;k++){
			int A,B; cin>>A>>B;
			for(int i=1000;i>=0;i--){
				for(int j=1;j<=B && i+A*j<=1000;j++){
					dp[i+A*j] += dp[i];
				}
			}
		}
		int G; cin>>G;
			for(int i=0;i<G;i++){
				int N; cin>>N;
				printf("%d\n",dp[N]);
			}
	}
	return 0;
}