#include<queue>
#include<stack>
#include<math.h>
#include<cmath>
#include<bitset>
#include<stdio.h>
#include<string>
#include<map>
#include<algorithm>
#include<vector>
#include<iostream>
#include<utility>
#include<functional>
#include<deque>
#include<list>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
int main(){
  while(1){
    int m;
    cin >> m;
    if(m==0){
      return 0;
    }
    int a[10],b[10],g,n,i,j,k,l;
    for(i=0;i<m;i++){
      cin >> a[i] >> b[i];
    }
    cin >> g;
    for(i=0;i<g;i++){
      cin >> n;
      ll dp[10][1010];
      for(j=0;j<=m;j++){
        for(k=0;k<=n;k++){
          dp[j][k]=0;
        }
      }
      dp[0][0]=1;
      for(j=1;j<=m;j++){
        for(k=0;k<=n;k++){
          for(l=0;l<=b[j-1];l++){
            int v=a[j-1]*l;
            if(k-v>=0){
              dp[j][k]+=dp[j-1][k-v];
            }
          }
        }
      }
      printf("%lld\n",dp[m][n]);
    }
  }
  return 0;
}
