#include<bits/stdc++.h>
using namespace std;
#define r(i,n) for(int i=0;i<n;i++)
int dp[8][2201],a,b,n,m;
main(){
  while(cin>>n,n){
    memset(dp,0,sizeof(dp));
    dp[0][0]=1;
    r(i,n){
      cin>>a>>b;
      r(j,1001)r(k,b)dp[i+1][j+(k+1)*a]+=dp[i][j];
      r(j,1001)dp[i+1][j]+=dp[i][j];
    }
    cin>>m;
    while(m--){
      cin>>a;
      cout<<dp[n][a]<<endl;
    }
  }
}