#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define all(a)  (a).begin(),(a).end()
#define pb push_back
#define INF 99999999



int main(){
    int n,g;
    while(cin>>n&&n){
        vector<pii> card;
        int sum=0;
        rep(i,n){
            int a,b;
            cin>>a>>b;
            card.pb(pii(a,b));
        }
        
        ll dp[8000]={};
        dp[0]=1;
        for(int i=1;i<=n;i++){
            for(int j=8000-1;j>=1;j--){
                for(int k=1;k<=card[i-1].second;k++){
                    if(j-k*card[i-1].first>=0){
//                        if(dp[j-k*card[i-1].first]>0)cout<<i<<" "<<j<<" "<<k<<" "<<"dp["<<j<<"]="<<dp[j]<<"+"<<dp[j-k*card[i-1].first]<<" "<<dp[j]+dp[j-k*card[i-1].first]<<endl;
                        dp[j]+=dp[j-k*card[i-1].first];
                    }
                }
            }
        }

        cin>>g;
        rep(i,g){
            int m;
            cin>>m;
            cout<<dp[m]<<endl;
            
        }
    }
    
}