#include <iostream>
#include <vector>
using namespace std;

typedef pair<int, int> C;

int m, g;
vector<C> cs;

int solve(int n, int index) {
    //cout << n << endl;
    if (n < 0) return 0;
    if (index == cs.size()) {
        if (n == 0) return 1;
        return 0;
    }
    int ans = 0;
    for (int i = 0; i <= cs[index].second; i++) {
        cs[index].second -= i;
        ans += solve(n - cs[index].first * i, index+1);
        cs[index].second += i;
    }
    return ans;
}

int main() {
    while (cin >> m, m) {
        cs.clear(); cs.resize(m);
        for (int i = 0; i < m; i++) cin >> cs[i].first >> cs[i].second;
        //for (int i = 0; i < m; i++) cout << cs[i].first << ' ' << cs[i].second << endl;
        cin >> g;
        for (int i = 0; i < g; i++) {
            int n; cin >> n;
            cout << solve(n, 0) << endl;
        }
        //cout << endl;
    }
    return 0;
}