#include <iostream>
#include <fstream>
#include <algorithm>
#include <cassert>
#include <cctype>
#include <complex>
#include <cstdio>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

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

int m,a[7],b[7],g,n;

int f(int r,int c){
	if(c==m)return r==0;
	int res=0;
	rep(i,b[c]+1)res+=f(r-a[c]*i,c+1);
	return res;
}

int main(){
	while(cin>>m&&m){
		rep(i,m)cin>>a[i]>>b[i];
		cin>>g;
		rep(i,g){
			cin>>n;
			cout<<f(n,0)<<endl;
		}
	}
}