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

#define loop(i,a,b) for(int i=(a); i<int(b); i++)
#define rep(i,b) loop(i,0,b)

typedef vector<int> vi;

vi ans;

void solve(vi const& a, vi const& k){
    int n=a.size();
    vi prev(1001);
    prev[0]=1;
    rep(i,n){
        vi cur(1001);
        rep(j,1001){
            rep(l,k[i]+1){
                int t=j+l*a[i];
                if(t<1001) cur[t]+=prev[j];
            }
        }
        swap(prev,cur);
    }
    swap(ans,prev);
}

int main(){
    int m;
    while(cin >> m,m){
        vi a(m), k(m);
        rep(i,m) cin >> a[i] >> k[i];
        solve(a,k);
        int g; cin >> g;
        rep(i,g){
            int t; cin>>t;
            cout << ans[t] << endl;
        }
    }
}