#include <bits/stdc++.h>

const int MOD = 1e9 + 7;
const int iINF = 1e9;
const long long int llINF = 1e18;
const double PI = acos(-1.0);
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, init, n) for (int i = init; i < (n); i++)

using namespace std;
using ll = long long int;
using P = pair<int, int>;
using T = tuple<int, int, int>;
using edge = struct
{
    int to;
    int cost;
};

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

int dp[101][1001]; // i, sum

int main()
{
    int M;
    
    while(cin >> M, M)
    {
        vector<int> a(M), b(M);
        rep(i, M) cin >> a[i] >> b[i];

        memset(dp, 0, sizeof(dp));

        dp[0][0] = 1;

        rep(i, M)
        {
            rep(j, b[i] + 1)
            {
                rep(k, 1001)
                {
                    if(k + j * a[i] <= 1000) dp[i + 1][k + j * a[i]] += dp[i][k];
                }
            }
        }

        int G;
        cin >> G;

        rep(i, G)
        {
            int N;
            cin >> N;
            cout << dp[M][N] << endl; 
        }
    }

    return 0;
}
