#include <bits/stdc++.h>
using namespace std;

const int MAXM = 8;
const int SIZE = 10001;

int m;
int a[MAXM], b[MAXM];
int dp[SIZE];

void rec(int sum, int i, int j) {
  if(i == m) {
    dp[sum] += 1;
    return;
  }
  rec(sum, i+1, 0);
  if(j < b[i]) rec(sum+a[i], i, j+1);
}

int main() {
  while(cin >> m && m) {
    for(int i = 0; i < m; ++i) {
      cin >> a[i] >> b[i];
    }
    fill(dp, dp+SIZE, 0);
    rec(0,0,0);

    int g;
    cin >> g;
    while(g--) {
      int n;
      cin >> n;
      cout << dp[n] << endl;
    }
  }
  return 0;
}