#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <numeric>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <utility>
#include <sstream>
#include <complex>
#include <fstream>

using namespace std;

#define FOR(i,a,b) for(long long i=(a);i<(b);i++)
#define REP(i,N) for(long long i=0;i<(N);i++)
#define ALL(s) (s).begin(),(s).end()
#define fi first
#define se second

#define PI acos(-1.0)
#define INF 1000000007
#define MOD 1000000007
#define EPS 1e-10
#define MAX_N 100100
#define MAX_M 100100

typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<double, double> PD;
typedef pair<string, ll> PS;
typedef vector<ll> V;
typedef pair<P, char> PC;
typedef pair<int, string> PSI;

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

int table[10010];

void print_table(){
	REP(i, 100){
		cout << table[i] << endl;
	}
}

void solve(){
	REP(i, m){
		for (int j = 10010 - 1; j >= 0; j--){
			if (table[j]){
				FOR(k, 1, b[i] + 1){
					table[j + a[i] * k] += table[j];
				}
			}
		}
	}
}


int main(){
	while (cin >> m&&m){
		fill(table, table + 10010, 0);
		table[0] = 1;
		REP(i, m){
			cin >> a[i] >> b[i];
		}
		solve();
		cin >> g;
		REP(i, g){
			cin >> n;
			cout << table[n] << endl;
		}

	}
}