#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> P;

#define pb push_back
#define rep(i, a, n) for(int i = (a); i < (n); i++)
#define dep(i, a, n) for(int i = (a); i >= (n); i--)
#define mod 1e9+7

__attribute__((constructor))
void initial() {
  cin.tie(0);
  ios::sync_with_stdio(false);
}

struct T {
  int s, n;
};


int main() {
  int m;
  while(cin >> m) {
    if(!m) return 0;
    T card[7] = {};
    rep(i, 0, m) {
      int a, b;
      cin >> a >> b;
      card[i].s = a;
      card[i].n = b;
    }
    int pat[7001] = {};
    rep(i, 0, m) {
      dep(j, 7000, 0) {
        if(pat[j]) {
          rep(k, 1, card[i].n + 1) pat[j + card[i].s * k] += pat[j];
        }
      }
      rep(j, 1, card[i].n + 1) pat[card[i].s * j]++;
    }

    int g;
    cin >> g;
    rep(i, 0, g) {
      int n;
      cin >> n;
      cout << pat[n] << endl;
    }
  }
}