#include <bits/stdc++.h>

using namespace std;

int main()
{
  int m, g;
  while(cin >> m, m){
    int a[7], b[7];
    for(int i = 0; i < m; i++) cin >> a[i] >> b[i];
    int comb[1001] = {0};
    comb[0] = 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 + k*a[i] <= 1000) comb[j + k*a[i]] += comb[j];
	}
      }
    }
    cin >> g;
    while(g--){
      int n;
      cin >> n;
      cout << comb[n] << endl;
    }
  }
  return 0;
}