#include <iostream>
#include <cstring>
#include <vector>
#include <map>
#include <algorithm>
#define fr first
#define sc second
using namespace std;

int n,p,dp[1001];
typedef pair<int,int> iP;
vector <iP> v;

void rec(int idx,int sum){
  if(sum > 1000) return;
  if(idx == n){ dp[sum]++; return; }
  for(int i=0;i<=v[idx].sc;i++){
    rec(idx+1,sum + (v[idx].fr * i));
  }
}

int main(){
  int m;
  while(cin >> n,n){
    v.resize(0);  memset(dp,0,sizeof(dp));
    for(int i=0;i<n;i++){
      int t,m; cin >> t >> m; v.push_back(iP(t,m));
    }
    cin >> m;
    rec(0,0);
    for(int i=0;i<m;i++){
      cin >> p;
      cout << dp[p] << endl;
    }
  }
}