#include <iostream>
#include <algorithm>
using namespace std;

int m, n;
int a[7], b[7];

int rec(int p, int sum) {
  if(p == m) return sum == n;
  int cnt = 0;
  for(int i = 0; i <= b[p]; ++i)
    cnt += rec(p+1,sum+a[p]*i);
  return cnt;
}

int main() {
  while(cin >> m && m) {
    for(int i = 0; i < m; ++i) cin >> a[i] >> b[i];
    int g;
    cin >> g;
    while(g--) {
      cin >> n;
      cout << rec(0,0) << endl;
    }
  }
  return 0;
}