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

int m, g, n;
vector<int> A, B;
long long dp[10][8000];

long long solve(int k, int sum, int goal) {
    if (0 <= dp[k][sum]) {
        return dp[k][sum];
    }
    if (sum == goal) {
        return 1;
    }
    if (goal < sum || (int)A.size() <= k) {
        return 0;
    }
    long long ret = 0;
    for (int i=0; i<=B[k]; ++i) {
        ret += solve(k+1, sum + A[k] * i, goal);
    }
    return dp[k][sum] = ret;
}

int main() {
    while (cin >> m, m) {
        A.resize(m);
        B.resize(m);
        for (int i=0; i<m; ++i) {
            cin >> A[i] >> B[i];
        }
        cin >> g;
        for (int i=0; i<g; ++i) {
            cin >> n;
            memset(dp, -1, sizeof dp);
            cout << solve(0, 0, n) << endl;
        }
    }
    return 0;
}