#include <cstdio>
#include <cstring>

int main()
{
	int m;
	while (scanf("%d", &m), m)
	{
		int a[7], b[7];
		for (int i = 0; i < m; ++i)
			scanf("%d %d", a+i, b+i);

		int combi[1001];
		memset(combi, 0, sizeof(combi));
		combi[0] = 1;
		for (int i = 0; i < m; ++i)
		{
			int c[1001];
			memset(c, 0, sizeof(c));
			for (int j = 1; j <= b[i]; ++j)
				for (int k = 1000 - j*a[i]; k >= 0; --k)
					if (combi[k])
						c[k + j*a[i]] += combi[k];
			for (int i = 0; i < 1001; ++i)
				combi[i] += c[i];
		}
		int g;
		scanf("%d", &g);
		while (g--)
		{
			int n;
			scanf("%d", &n);
			printf("%d\n", combi[n]);
		}
	}

	return 0;
}