#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;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8Y3N0ZGlvPgojaW5jbHVkZSA8Y3N0cmluZz4KI2RlZmluZSBsbCBsb25nIGxvbmcKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKY29uc3QgaW50IG1heG4gPSAxMDAwMDA7CmxsIGNbNV0scSxkWzVdLHYsZHBbbWF4bisxXTsKdm9pZCBzb2x2ZSgpCnsKCWZvciAoaW50IGk9MTsgaTw9NDsgaSsrKQoJCWNpbiA+PiBjW2ldOwoJY2luID4+IHE7CgltZW1zZXQoZHAsMCxzaXplb2YoZHApKTsKCWRwWzBdPTE7Cglmb3IgKGludCBpPTE7IGk8PTQ7IGkrKykKCQlmb3IgKGludCBqPWNbaV07IGo8PW1heG47IGorKykKCQl7CgkJCS8vY291dCA8PCBpKmogPDwgJ1xuJ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