#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
#include <utility>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <functional>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cassert>
 
using namespace std;
 
#define REP(i,s,e) for(int i=(s); i<(int)(e); i++)
#define rep(i,n) REP(i, 0, n)
#define pb push_back
#define mp make_pair
#define all(r) (r).begin(),(r).end()
#define rall(r) (r).rbegin(),(r).rend()
#define fi first
#define se second
 
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
 

 
const int INF = 1e9;
const double EPS = 1e-8;
const int mod = 1e9 + 7;
const ll LINF = 1e18;
const double PI = acos(-1);

struct Input
{
	int n;
	char c;
	char str[1024];
	string s;
	ll l;
	int nextInt(){
		scanf("%d", &n);
		return n;
	}
	ll nextLong(){
		scanf("%lld", &l);
		return l;
	}
	char nextChar(){
		scanf("%c", &c);
		return c;
	}
	string next(){
		scanf("%s", str);
		return string(str);
	}
	string nextLine(){
		getline(cin, s);
		return s;
	}
};
Input in;

int a[15], b[15];
ll dp[15][2500];

int main(){
	while(1){
		int m = in.nextInt();
		if(m == 0) break;

		rep(i, m) a[i] = in.nextInt(), b[i] = in.nextInt();
		rep(i, 15) rep(j, 2500) dp[i][j] = 0LL;

		dp[0][0] = 1LL;
		rep(i, m){
			for(int j = 0; j < 2500; j++){
				if(dp[i][j] == 0LL) continue;
				for(int k = j, l = 0; k < 2500 && l <= b[i]; k += a[i], l++){
					dp[i + 1][k] += dp[i][j];
				}
			}
		}
		// for(int i = 0; i < m + 1; i++){
		// 	for(int j = 0; j < 26; j++){
		// 		printf("%4lld", dp[i][j]);
		// 	}
		// 	printf("\n");
		// }

		int g = in.nextInt();
		rep(i, g){
			int n = in.nextInt();
			printf("%lld\n", dp[m][n]);
		}
	}
}