#include <cstdio>
using namespace std;

int main()
{
	int dp[10001];
	
	int m;
	while (scanf("%d", &m), m){
		dp[0] = 1;
		for (int i = 1; i <= 1000; i++){
			dp[i] = 0;
		}
		
		for (int i = 0; i < m; i++){
			int number, num;
			scanf("%d %d", &number, &num);
			
			for (int j = 1000; j >= 0; j--){
				if (dp[j]){
					for (int k = 1; k <= num; k++){
						dp[j + k * number] += dp[j];
					}
				}
				
			}
		}
		
		int g;
		scanf("%d", &g);
		for (int i = 0; i < g; i++){
			int n;
			scanf("%d", &n);
			
			printf("%d\n", dp[n]);
		}
	}
	
	return 0;
}