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

#define rep(i,n) for(int i=0;i<n;i++)
#define REP(n) rep(i,n)
#define all(n) n.begin(),n.end()

const int MAXM = 7, MAXB = 10, MAXN = 1001, MAXC = 71;

int dp[MAXM + 1][MAXN + 10];//dp[i][j] i番目までのカードでjを作る場合の数
int a[MAXM];
int b[MAXM];


int m;

int main()
{
    while(cin >> m && m)
    {
        REP(m){cin >> a[i] >> b[i];}

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

        REP(m)rep(j,MAXN)
        {
            dp[i+1][j] += dp[i][j];
            for(int k = 1;k <= b[i];k++)if(j+ a[i]*k <= MAXN)
                dp[i+1][j+a[i] * k] += dp[i][j];

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