#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using pii = pair<int, int>;

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(10);
  
  int m;
  while (cin >> m, m) {
    int a[7], b[7];
    for (int i = 0; i < m; i++) {
      cin >> a[i] >> b[i];
    }
    int dp[8][1010] = {};
    dp[0][0] = 1;
    for (int i = 0; i < m; i++) {
      for (int j = 0; j <= 1000; j++) {
        for (int k = 0; k <= b[i] && j - a[i] * k >= 0; k++) {
          dp[i + 1][j] += dp[i][j - a[i] * k];
        }
      }
    }
    int g;
    cin >> g;
    for (int i = 0; i < g; i++) {
      int n;
      cin >> n;
      cout << dp[m][n] << endl;
    }
  }

  return 0;
}
