#include <bits/stdc++.h>
using namespace std;
int main() {
  int m, g, n, a[10], b[10];
  while (cin >> m, m) {
    for (int i = 0; i < m; i++) cin >> a[i] >> b[i];
    int dp[1010] = {1};
    for (int i = 0; i < m; i++)
      for (int j = 1000; j >= 0; j--)
        for (int k = 1; k <= b[i]; k++)
          if (j + a[i] * k <= 1000) dp[j + a[i] * k] += dp[j];
    cin >> g;
    while (g--) cin >> n, cout << dp[n] << endl;
  }
  return 0;
}
