#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>

using namespace std;

int exist[100000];

int main(){
	
	while(true){
		
		int m;
		cin >> m;
		if(m == 0){
			break;
		}
		
		for(int i = 0; i < 10000; i++){
			exist[i] = 0;
		}
		
		exist[0] = 1;
		
		int a, b;
		
		for(int loop = 0; loop < m; loop++){
			cin >> a >> b;
			for(int i = 9999; i >= 0; i--){
				if(exist[i] > 0){
					for(int j = 1; j <= b; j++){
						exist[i + a * j] += exist[i];
					}
				}
			}
		}
		
		int g;
		
		cin >> g;
		
		for(int loop = 0; loop < g; loop++){
			int n;
			cin >> n;
			cout << exist[n] << endl;
		}
		
	}
	return 0;
}