#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<cctype>
using namespace std;
#define rep(i,n) for(int i = 0 ; i < (n) ; ++i )
int main(){
  int n,dp[8][1001],g,m;
  while(cin >> n , n){
    int a[n],b[n];
    fill_n(dp[0],8008,0);
    dp[0][0] = 1;
    rep(i,n) cin >> a[i] >> b[i];
    for(int i = 0 ; i < n ; i++ ){
      for(int j = 0 ; j < 1001 ; j++ ){
        for(int k = 0 ; k <= b[i] ; k++){
          if( j + a[i] * k > 1000) break;
          dp[i+1][j + a[i] * k] += dp[i][j];
        }
      }
    }
    cin >> g;
    while(g--){
      cin >> m;
      cout << dp[n][m] << endl;
    }
  }
}