#include <iostream>
using namespace std;

int main(void)
{
  while (1) {
    int g, m;
    int table[1001] = {};
    table[0] = 1;

    cin >> m; if (!m) break;
    for (int i=0; i<m; i++) {
      int a, b; cin >> a >> b;
      for (int j=1000; j>=0; j--) {
	if (!table[j]) continue;
	for (int k=1; k<=b; k++) {
	  if (j+a*k > 1000) break;
	  table[j+a*k] += table[j];
	}
      }
    }

    cin >> g;
    for (int i=0; i<g; i++) {
      int n; cin >> n;
      cout << table[n] << endl;
    }
  }

  return 0;
}