#include <bits/stdc++.h>
using namespace std;

int main() {
    int m;
    while(cin >> m, m) {
        vector<int> a(m), b(m);
        for(int i=0; i<m; ++i) {
            cin >> a[i] >> b[i];
        }
        vector<vector<int>> dp(m+1, vector<int>(1001));
        dp[0][0] = 1;
        for(int i=0; i<m; ++i) {
            for(int j=0; j<1001; ++j) {
                for(int k=0; k*a[i]+j<1001 && k<=b[i]; ++k) {
                    dp[i+1][j+k*a[i]] += dp[i][j];
                }
            }
        }
        int g;
        cin >> g;
        while(g--) {
            int n;
            cin >> n;
            cout << dp[m][n] << endl;
        }
    }
}
