#include<iostream>
#include<vector>
#include<cstring>

using namespace std;

typedef pair<int,int> P;
const int MAX = 1001;

int Ans[8][MAX];
vector<P> V;

void make(){
  for(int i = 0; i < V.size(); i++)
    for(int k = 0; k < MAX; k++)
      if(Ans[k])
	for(int j = 0; j <= V[i].second; j++){
	  if(k+V[i].first*j >= MAX)break;
	  Ans[i+1][k+V[i].first*j] += Ans[i][k];
	}
}

void init(){
  memset(Ans,0,sizeof(Ans));
  Ans[0][0] = 1;
  V.clear();
}

int main(){

  int m;
  while(cin >> m && m){
    init();
    for(int i = 0; i < m; i++){
      P p;
      cin >> p.first >> p.second;
      V.push_back(p);
    }

    make();

    int g;
    cin >> g;
    while(g--){
      int n;
      cin >> n;
      cout << Ans[V.size()][n] << endl;
    }

  }
  return 0;
}