#include<iostream>
using namespace std;
int a[7],b[7];
int N;
int dfs(int U,int id)
{
	if(id==N)return U==0;
	int ret=0;
	for(int k=0;k<=b[id];k++)
	{
		if(U<k*a[id])break;
		ret+=dfs(U-k*a[id],id+1);
	}
	return ret;
}
main()
{
	while(cin>>N,N)
	{
		for(int i=0;i<N;i++)cin>>a[i]>>b[i];
		int Q;cin>>Q;
		for(;Q--;)
		{
			int g;cin>>g;cout<<dfs(g,0)<<endl;
		}
	}
}

