#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

#define NMAX 1000

int main() {
  int m, g, n, s;
  int a[7], b[7], dp[2][NMAX+1];
  while (1) {
    scanf("%d", &m);
    if (!m) break;
    for (int i=0; i<m; i++) {
      scanf("%d %d", &a[i], &b[i]);
    }

    memset(dp, 0, sizeof(dp));
    dp[0][0] = 1;
    s = 0;
    for (int i=0; i<m; i++) {
      memcpy(dp[!(i&1)], dp[i&1], sizeof(dp[0]));
      for (int j=0; j<=s; j++) {
        if (!dp[i&1][j]) continue;
        for (int k=0,l=j+a[i]; k<b[i] && l<=NMAX; k++,l+=a[i]) {
          dp[!(i&1)][l] += dp[i&1][j];
        }
      }
      s += a[i]*b[i];
      s = min(s, NMAX);
    }

    scanf("%d", &g);
    for (int i=0; i<g; i++) {
      scanf("%d", &n);
      printf("%d\n", dp[m&1][n]);
    }
  }

  return 0;
}