#include <iostream>
#include <sstream>
#include <vector>
#include <list>
#include <string>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <algorithm>
#include <numeric>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
using namespace std;

typedef pair <int, int> PII;
typedef vector <PII> VPII;
typedef vector <int> VI;

int dp[10][10000];

int main() {
    int m;
    while ( cin >> m && m ) {
        VPII v( m );
        for ( int i = 0; i < m; ++ i ) {
            cin >> v[i].first >> v[i].second;
        }
        for ( int i = 0; i < 10; ++ i ) {
            for ( int j = 0; j < 10000; ++ j ) {
                dp[i][j] = 0;
            }
        }
        dp[0][0] = 1;
        for ( int i = 0; i < m; ++ i ) {
            for ( int j = 0; j < 10000; ++ j ) {
                for ( int k = 0; k <= v[i].second; ++ k ) {
                    int s = j + v[i].first * k;
                    if ( s >= 10000 ) continue;
                    dp[i+1][s] += dp[i][j];
                }
            }
        }
        int g;
        cin >> g;
        for ( int i = 0; i < g; ++ i ) {
            int n;
            cin >> n;
            cout << dp[m][n] << endl;
        }
    }
    return 0;
}