#include <bits/stdc++.h>
#define rep(i,a,n) for(int i=a;i<n;i++)
#define repb(i,a,b) for(int i=a;i>=b;i--)
#define all(a) a.begin(),a.end()
#define o(a) cout<<a<<endl
#define int long long
#define fi first
#define se second
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;


signed main(){
    int m;
    while(cin >> m, m){
        vi a(m), b(m);
        rep(i, 0, m){
            cin >> a[i] >> b[i];
        }
        int dp[100][1001] = {0};
        dp[0][0] = 1;
        rep(i, 1, m + 1) {
            //cout << " " << a[i - 1] << " " << b[i - 1] <<endl;
            rep(k, 0, 1000){
                dp[i][k] = dp[i - 1][k];
            }
            rep(j, 1, b[i - 1] + 1){
                rep(k, 0, 1000){
                    if(k - a[i - 1]*j >= 0){
                        dp[i][k] += dp[i - 1][k - a[i - 1]*j];
                    }
                }
            }
            /*cout << " " << i << " ";
            rep(j, 0, 15){
                cout<<dp[i][j]<<" ";
            }
            cout << endl;*/
        }
        /*rep(i, 0, 15){
            cout<<dp[m][i]<<" ";
        }
        cout<<endl;*/
        int g;
        cin >> g;
        rep(i, 0, g){
            int n;
            cin >> n;
            cout << dp[m][n] <<endl;
        }
    }
}