#include <iostream>
#include <algorithm>

using namespace std;

int m;
int a[7], b[7];
int g;
int dp[8][1001];
int n;

int solve(){
  fill(dp[0], dp[8], 0);
  dp[0][0] = 1;
  for(int i=0;i<m;i++){
    for(int j=0;j<1001;j++){
      if(dp[i][j] == 0) continue;
      for(int k=0;k<=b[i];k++){
	int tmp = j+a[i]*k;
	if(tmp > 1000) continue;
	dp[i+1][tmp] += dp[i][j];
      }
    }
  }
}

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