#include<iostream>
#include<vector>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<sstream>
#include<cmath>
#include<numeric>
#include<map>
#include<stack>
#include<queue>
using namespace std;
int inf = 1000000000;

int main(void) {

  int m;
  while( cin >> m ){
    if( m == 0 ) break;
    vector<int> a(m);
    vector<int> b(m);
    for(int i=0; i<m; i++) cin >> a[i] >> b[i];

    vector< vector<int> > dp(m+1, vector<int> (1001, 0));
    dp[0][0] = 1;
    for(int i=0; i<m; i++){
      for(int j=0; j<1001; j++){
        for(int k=0; k<=b[i]; k++){
          if( j - a[i] * k < 0 ) continue;
          dp[i+1][j] += dp[i][ j-a[i]*k ];
        }
      }
    }
    int g;
    cin >> g;
    for(int i=0; i<g; i++){
      int n; cin >> n;
      cout << dp[m][n] << endl;
    }

  }

  return 0;
}

// EOF