#include <iostream>
#include <cstdio>
#include <cmath>
#include <queue>
#include <map>
#include <string>
#define INF 1<<27
#define ESP 1e-10

using namespace std;

int main()
{
  int dp[8][1001];
  int n,m,a[1000],t[1000];

  while( cin >> m,m){

    for(int i = 0 ; i < m ; i++ )
      {
	cin >> a[i] >> t[i];
      }
    /*
      fill(dp[0],dp[8],0);
      dp[0][0] = 1;

      for(int i=0 ; i<m ; i++ )
      {
      for(int j = 0 ; j <= 1000 ; j++ )
      {
      if( dp[i][j] ){
      for(int k = 0 ; k <= t[i]  ; k++ ){
      if(j + a[i]*k <= 1000)dp[i+1][j+k*a[i]] += dp[i][j];
      }
      }
      }
      }*/
    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] ) {
	  for( int k=0; k<=t[i]; k++ ) {
	    if( j+k*a[i] <= 1000 ) dp[i+1][j+k*a[i]] += dp[i][j];
	  }
	}
      }
    }
    /*for(int i = 0 ; i <= 7 ; i++){
      for(int j = 0 ; j <= 20;j++  ){
      cout << dp[i][j] << " ";
      }
      cout << endl;
      }*/
    int s;
    cin >> s;
    for(int i = 0 ; i < s ; i++ )
      {
	cin >> n;
	cout << dp[m][n] << endl;
      }
  }
  return 0;
}