#include <iostream>
#include <iomanip>
#include <vector>
#include <utility>

using namespace std;

int saiki(vector<vector<pair<int,int> > > &cards,int ind,int m,int moku,int x){
    if(m > moku)return 0;
    if(ind==cards.size() && m==moku) return x;
    if(ind==cards.size()) return 0;

    int t = 0;
    for(int i=0;i<cards[ind].size();i++){
        t += saiki(cards,ind+1,m+cards[ind][i].first,moku,x*1);
    }
    return t;
}
int sub(vector<vector<pair<int,int> > >cards,int x){
    return saiki(cards,0,0,x,1);
}
int cnk(int n,int k){
    int x = 1;
    for(int i=0;i<k;i++) x *= (n-i);
    for(int i=0;i<k;i++) x /= (k-i);
    return x;
}
int solve(int m){
    vector<pair<int,int> > cards(m);
    for(int i=0;i<m;i++){
        int a,b;
        cin >> a >> b;
        cards[i] = make_pair(a,b);
    }
    vector<vector<pair<int,int> > > cards2(m);
    for(int i=0;i<m;i++){
        for(int j=0;j<cards[i].second+1;j++){
            cards2[i].push_back(make_pair(cards[i].first*j,cnk(cards[i].second,j)));
        }
    }
    int g;
    cin >> g;
    for(int i=0;i<g;i++){
        int x;
        cin >> x;
        cout << sub(cards2,x) << endl;
    }
}

int main(){
    while(true){
        int m;
        cin >> m;
        if(m==0)break;
        solve(m);
    }
    return 0;
}