#include <iostream>
using namespace std;
int dp[8][1001] = {0};
int a[7],b[7],m;

void comb() {
  for(int i=0;i<8;i++) {
    for(int j=0;j<1001;j++) {
      dp[i][j] = 0;
    }
  }

  dp[0][0] = 1;

  for(int i=0;i<m;i++) {
    for(int j=0;j<1001;j++) {
      for(int k=0;j-k*a[i]>=0 && k <= b[i];k++) {
        dp[i+1][j] += dp[i][j-k*a[i]];
      }
      //        cout << "dp[" << i+1 << "][" << j << "] = " << dp[i+1][j] << endl;
    }
  }
}

int main() {
  int g,n;
  while(cin >> m) {
    if(m==0) break;
    for(int i=0;i<m;i++) {
      cin >> a[i] >> b[i];
    }
    comb();
    cin >> g;
    for(int i=0;i<g;i++) {
      cin >> n;
      cout << dp[m][n] << endl;
    }
  }
}