#include <stdio.h>
#include <string.h>

int main(void){
  int i,j,k;
  int n,m,val,a[7],b[7];
  int dp[8][1001];

  while(scanf("%d",&n), n){
    for(i=0;i<n;i++){
      scanf("%d%d",&a[i],&b[i]);
    }

    memset(dp,0,sizeof(dp));
    dp[0][0] = 1;

    //J[hÌÔ
    for(i=0;i<n;i++){
      //v_
      for(j=0;j<=1000;j++){
        //iÔÚÌJ[hð½gp·é©
        for(k=0;k<=b[i];k++){
          if(j+a[i]*k>1000) continue; //1001ÈãÍ¢çÈ¢ÌÅ³
          dp[i+1][j+a[i]*k] += dp[i][j];
        }
      }
    }

    scanf("%d",&m);
    while(m--){
      scanf("%d",&val);
      printf("%d\n",dp[n][val]);
    }
  }

  return 0;
}