#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>

using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
typedef complex<double> P;
static const double EPS = 1e-8;

#define FOR(i,k,n) for (int i=(k); i<(int)(n); ++i)
#define REP(i,n) FOR(i,0,n)

int main(void){
  int m,g;
  int card[7];
  int num[7];
  while(cin>>m){
    if(m==0) break;
    REP(i,m){
      cin>>card[i]>>num[i];
    }
    cin>>g;
    int dp[1001]={};
    dp[0] = 1;
    for(int i=0; i<m; i++){
      for(int j=1000-card[i];j>=0;j--){
        for(int k=1; k<=num[i]&&j+k*card[i]<=1000;k++){
          dp[j+k*card[i]] += dp[j];
        }
      }
    }
    while(g--){
      int n;
      cin>>n;
      cout<<dp[n]<<endl;
    }
  }
  return 0;
}