#include<iostream>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
int m,g,a[7],b[7],c,n;
void rec(int k,int s){
	rep(i,b[k]+1){
		int t=s+a[k]*i;
		if(t<n&&k<m-1)rec(k+1,t);
		if(t==n)c++;
	}
}
int main(){
	while(cin>>m,m){
		rep(i,m)cin>>a[i]>>b[i];
		cin>>g;
		rep(i,g){
			c=0; cin>>n;
			rec(0,0);
			cout<<c<<endl;
		}
	}
	return 0;
}