#include <iostream>
#include <cstdio>
#include <cstring>
#define ll long long
using namespace std;
const int maxn = 100000;
ll c[5],q,d[5],v,dp[maxn+1];
void solve()
{
	for (int i=1; i<=4; i++)
		cin >> c[i];
	cin >> q;
	memset(dp,0,sizeof(dp));
	dp[0]=1;
	for (int i=1; i<=4; i++)
		for (int j=c[i]; j<=maxn; j++)
		{
			//cout << i*j << '\n';
			dp[j]+=dp[j-c[i]];
		}
	//cout << dp[c[1]*2] << '\n';
	//cout << "Done\n";
	for (int i=1; i<=q; i++)
	{
		for (int j=1; j<=4; j++)
			cin >> d[j];
		cin >> v;
		ll res=0;
		for (int j=0; j<16; j++)
		{
			int dem=0;
			int dv=v;
			int jj=j;
			int vt=0;
			while (jj!=0)
			{
				vt++;
				dem+=jj%2;
				if (jj%2==1) 
					{
						dv-=(d[vt]+1)*c[vt];
						//cout << vt << ' ';
					}
				jj/=2;
			}
			//cout << dv  << ' ';
			if (dv>=0)
			{
				if (dem%2==0) res+=dp[dv];
				else res-=dp[dv]; 
			}
		}
		cout << res << '\n';
	}
}
int main()
{ 
	ios_base::sync_with_stdio(0);
	//freopen("11259.INP","r",stdin);
	int t;
	cin >> t;
	for (int i=1; i<=t; i++)
	{
		//cout << i << '\n';
		solve();
	}
	return 0;
}