#include <iostream>
#include <algorithm>
#include <string.h>
#include <queue>
#include <cstdio>
#define rep(i,k,n) for(int i = k ; i < n ; i++ )
#define INF 10000000

using namespace std;

int main(){
  int n,K;
  int a[7],m[7];
  int dp[8][1001];
  while( cin >> n,n){
    for(int i = 0 ; i < n ; i++ )cin >> a[i] >> m[i];
    fill(dp[0],dp[8],0);
    dp[0][0] = 1;

    for(int i = 0 ; i < n ; i++ ){
      for(int j = 0 ; j < 1000 ; j++ ){
	for(int k = 0 ; k <= m[i] ; k++ ){
	  if(j + a[i]*k > 1000)continue;
	  dp[i+1][j+a[i]*k] += dp[i][j]; 
	}
      }
    }
    int g;
    cin >> g; 
    rep(i,0,g){
      cin >> K;
      cout << dp[n][K] << endl;
    }
  }
  return 0;
}