#include <iostream>
#include <vector>
#include <sstream>
#include <queue>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)

int m,num[7],val[7],n,solve;

void dfs(int sum,int pos){

	if(sum == n && pos == m)solve++;

	if(sum > n || pos >= m)return;
	
	for(int i=0;i<=val[pos];i++){
		dfs(sum+(i*num[pos]) , pos+1);
	}
}
int main(){
	while(cin >> m ,m){
		rep(i,m){
			int a,b;
			cin >> num[i] >> val[i];
		}
		
		int g; cin >> g;
		rep(i,g){
			cin >> n;
			solve = 0 , dfs(0,0);
			cout << solve << endl;
		}
	}
}