#include <iostream>
#include <cstring>

using namespace std;

const int MAX_M = 7;
const int MAX_G = 10;
const int MAX_K = 1000;
int m;  // カードの種類 1<= m <= 7
int a[MAX_M], b[MAX_M]; // a: カードに書かれた数字 b: 枚数
int g; // ゲームの回数
int n[MAX_G];
int K;
int dp[MAX_M + 1][MAX_K + 1];

void solve()
{
    memset(dp, 0, sizeof(dp));
    dp[0][0] = 1;

    for (int i = 0; i < m; i++)
        for (int j = 0; j <= K; j++)
            for (int k = 0; k <= b[i] && k * a[i] <= j; k++)
                dp[i + 1][j] += dp[i][j - k * a[i]];
}

int main()
{
    while (cin >> m, m) {
        for (int i = 0; i < m; i++)
            cin >> a[i] >> b[i];
      
        K = 0;
        cin >> g;
        for (int i = 0; i < g; i++) {
            cin >> n[i];
            K = max(K, n[i]);
        }
        solve();

        for (int i = 0; i < g; i++)
            cout << dp[m][n[i]] << endl;
    }


    return 0;
}