#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <complex>
#include <cstring>
#include <cstdlib>
#include <string>
using namespace std;

#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()

int main() {
  int m;
  while(cin >> m, m) {
    int card[m], num[m];
    REP(i,m)
      cin >> card[i] >> num[i];
    int dp[m+1][1001];
    memset(dp,0,sizeof(dp));

    dp[0][0] = 1;
    for(int i=1; i<=m; ++i) {
      REP(j,1001)
        dp[i][j] = dp[i-1][j];
      for(int k=1; k<=num[i-1]; ++k) {
        for (int j=0; j + k*card[i-1] < 1001; ++j) {
          if (dp[i-1][j]>0) {
            dp[i][j + k*card[i-1]] += dp[i-1][j] ;
            //    printf("%d,%d,%d %d\n",i,j,j + k*card[i-1],dp[i][j + k*card[i-1]]);
          }
        }
      }
    }
    int g;
    cin >> g;
    while(g--) {
      int a;
      cin >> a;
      cout << dp[m][a] << endl;
    }
  }
}