#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<cstdio>
#include<climits>
#include<cmath>
#include<cstring>
#include<string>

#define f first
#define s second
#define mp make_pair

#define REP(i,n) for(int i=0; i<(int)(n); i++)
#define FOR(i,c) for(__typeof((c).begin()) i=(c).begin(); i!=(c).end(); i++)
#define ALL(c) (c).begin(), (c).end()

using namespace std;

typedef unsigned int uint;
typedef long long ll;

int ans[100*1000*10];

int nCk(int n,int k){
  long long ret = 1;
  REP(i,k) ret*=(n-i);
  REP(i,k) ret/=(i+1);
}

int main(){
  int n;
  while(scanf("%d",&n),n){
    pair<int,int> card[n];
    int mm = 0;
    REP(i,n) scanf("%d%d",&(card[i].f),&(card[i].s));
    sort(card,card+n);
    memset(ans,0,sizeof(ans));
    ans[0] = 1;
    REP(i,n){
      for(int j=mm; j>=0; j--){
	if(ans[j] != 0){
	  for(int k=1; k<=card[i].s; k++){
	    //printf("j=%d, j+k*card[i].f=%d ans[j]=%d\n",j,j+k*card[i].f,ans[j]);
	    ans[j+k*card[i].f] += ans[j];
	  }
	  mm = max(mm, j+card[i].s*card[i].f);
	}
      }
    }
    int g; cin>>g;
    REP(i,g){
      int gg; cin>>gg;
      cout << ans[gg] << endl;
    }
  }
  return 0;
}