#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <map>
using namespace std;

#define MAX 1001
#define f first
#define s second
typedef pair<int, int> P;

int main(){
  int m;
  while(cin >> m, m){
    vector<P> card(m);
    for(int i = 0 ; i < m ; i++){
      cin >> card[i].f >> card[i].s;
    }
    
    int dp[8][MAX] = {0};
    dp[0][0] = 1;
    for(int i = 0 ; i < m ; i++){
      for(int j = 0 ; j < MAX ; j++){
	for(int k = 0 ; k <= card[i].s ; k++){
	  if(card[i].f*k+j < MAX){
	    dp[i+1][card[i].f*k+j] += dp[i][j];
	  }
	}
      }
    }
    int g, ng;
    cin >> g;
    while(g--){
      cin >> ng; 
      cout << dp[m][ng] << endl;
    }
  }
  return 0;
}