#include <iostream>
#include <complex>
#include <sstream>
#include <string>
#include <algorithm>
#include <deque>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <vector>
#include <set>
#include <limits>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <ctime>
using namespace std;
#define REP(i, j) for(int i = 0; i < (int)(j); ++i)
#define FOR(i, j, k) for(int i = (int)(j); i < (int)(k); ++i)
#define SORT(v) sort((v).begin(), (v).end())
#define REVERSE(v) reverse((v).begin(), (v).end())
typedef pair<int, int> P;
const int L = 1010;

int main() {
  int N;
  while(cin >>N && N){
    vector<P> v(N);
    REP(i, N) cin >>v[i].first >>v[i].second;
    vector<int> cnt(L, 0);
    cnt[0] = 1;
    REP(i, N){
      vector<int> tmp(L, 0);
      REP(j, L){
        if(cnt[j] <= 0) continue;
        REP(k, v[i].second) if(j + v[i].first * (k + 1) < L) tmp[j + v[i].first * (k + 1)] += cnt[j];
      }
      REP(j, L) cnt[j] += tmp[j];
    }
    int M; cin >>M;
    REP(i, M){
      int p; cin >>p;
      cout <<cnt[p] <<endl;
    }
  }
  return 0;
}