#include<iostream>
#include<vector>
using namespace std;

struct Card{
  int value,num;
  Card(int a, int b){value=a;num=b;};
};

int main()
{
  int m;
  int a,b;
  int g;
  int n;

  while(cin>>m,m){
    vector<Card>card;
    int dp[7+1][7000]={0};

    for(int i=0;i<m;i++){
      cin>>a>>b;
      card.push_back(Card(a,b));
    }

    dp[0][0]=1;
    for(int i=0;i<m;i++){
      for(int j=0;j<=1000;j++){
	dp[i+1][j]+=dp[i][j];
	for(int k=1;k<=card[i].num;k++){
	  dp[i+1][j + card[i].value * k]+=dp[i][j];
	}
      }
    }

    cin>>g;
    for(int i=0;i<g;i++){
      cin>>n;
      cout<<dp[m][n]<<endl;
    }
  }
}