#include <iostream>
#include <string>
#include <algorithm>
#include <functional>
#include <vector>
#include <utility>
#include <cstring>
#include <iomanip>
#include <numeric>
#include <cmath>
#include <cassert>
using namespace std;
using ll = long long;
const int INF = 1<<30;
const int MOD = 1e9 + 7;
int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int m;
    while(cin >> m, m)
    {
        vector<int> a(m), b(m);
        for(int i = 0; i < m; i++) cin >> a[i] >> b[i];
        vector<int> dp(1010, 0);
        dp[0] = 1;
        for(int i = 0; i < m; i++)
        {
            for(int j = 1010; j >= 1; j--)
            {
                for(int k = 1; k <= b[i]; k++)
                {
                    if(j - a[i] * k >= 0) dp[j] += dp[j - a[i] * k];
                }
            }
        }
        int g; cin >> g;
        for(int i = 0; i < g; i++)
        {
            int n; cin >> n;
            cout << dp[n] << endl;
        }
    }
    return 0;
}


