#define _USE_MATH_DEFINES
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <cfloat>
#include <ctime>
#include <cassert>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <numeric>
#include <list>
#include <iomanip>
#include <iterator>
 
using namespace std;
 
typedef long long ll;
typedef pair<int,int> Pii;
typedef pair<ll,ll> Pll;
 
#define FOR(i,n) for(int i = 0; i < (n); i++)
#define sz(c) ((int)c.size())

typedef unsigned long long ull;
const int INF = (int)1e8;

int a[7],b[7];
int main(){
	int m;
	while(cin>>m,m){
		FOR(i,m) cin>>a[i]>>b[i];

		int dp[10000] = {};
		dp[0] = 1;
		FOR(i,m){
			for(int j = 1000; j >= 0; j--){
				for(int k = 1; k <= b[i]; k++){
					dp[j + k * a[i]] += dp[j];
				}
			}
		}

		int g; cin>>g;
		FOR(i,g){
			int x; cin>>x;
			cout << dp[x] << endl;
		}
	}
	return 0;
}