#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <numeric>

#define REP(i, a, b) for (int i = int(a); i < int(b); i++)
#ifdef _DEBUG_
#define dump(val) cerr << __LINE__ << ":\t" << #val << " = " << (val) << endl
#else
#define dump(val)
#endif

using namespace std;

typedef long long int ll;

template<typename T>
vector<T> make_v(size_t a, T b) {
    return vector<T>(a, b);
}
template<typename... Ts>
auto make_v(size_t a, Ts... ts) {
    return vector<decltype(make_v(ts...))>(a, make_v(ts...));
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int m;
    while (cin >> m, m) {
        int mx = 1001;
        vector<int> a(m), b(m);
        REP(i, 0, m) {
            cin >> a[i] >> b[i];
        }
        auto dp = make_v(m + 1, mx, 0LL);
        dp[0][0] = 1;
        REP(i, 0, m) {
            REP(j, 0, mx) {
                REP(k, 0, b[i] + 1) {
                    if (j + k * a[i] < mx) {
                        dp[i + 1][j + k * a[i]] += dp[i][j];
                    }
                }
            }
        }

        int g;
        cin >> g;
        REP(i, 0, g) {
            int n;
            cin >> n;
            cout << dp[m][n] << endl;
        }
    }
    return 0;
}

