#include <cstring>
#include <cmath>
#include <algorithm>
#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <fstream>
#include <queue>
#define rep(i, l, r) for(int i = l; i <= r; i++)
#define down(i, l, r) for(int i = l; i >= r; i--)
#define MS 123456
#define MAX 1037471823
#define Q 100000
using namespace std;
int c1, c2, c3, c4, t, d1, d2, d3, d4, s;
long long ans, w[MS];
int main()
{
scanf("%d%d%d%d%d", &c1, &c2, &c3, &c4, &t);
w[0] = 1;
rep(i, c1, MS-1) w[i] += w[i-c1];
rep(i, c2, MS-1) w[i] += w[i-c2];
rep(i, c3, MS-1) w[i] += w[i-c3];
rep(i, c4, MS-1) w[i] += w[i-c4];
while (t-- > 0)
{
scanf("%d%d%d%d%d", &d1, &d2, &d3, &d4, &s);
ans = w[s];
if (s-(d1+1)*c1 >= 0) ans -= w[s-(d1+1)*c1];
if (s-(d2+1)*c2 >= 0) ans -= w[s-(d2+1)*c2];
if (s-(d3+1)*c3 >= 0) ans -= w[s-(d3+1)*c3];
if (s-(d4+1)*c4 >= 0) ans -= w[s-(d4+1)*c4];
if (s-(d1+1)*c1-(d2+1)*c2 >= 0) ans += w[s-(d1+1)*c1-(d2+1)*c2];
if (s-(d1+1)*c1-(d3+1)*c3 >= 0) ans += w[s-(d1+1)*c1-(d3+1)*c3];
if (s-(d1+1)*c1-(d4+1)*c4 >= 0) ans += w[s-(d1+1)*c1-(d4+1)*c4];
if (s-(d2+1)*c2-(d3+1)*c3 >= 0) ans += w[s-(d2+1)*c2-(d3+1)*c3];
if (s-(d2+1)*c2-(d4+1)*c4 >= 0) ans += w[s-(d2+1)*c2-(d4+1)*c4];
if (s-(d3+1)*c3-(d4+1)*c4 >= 0) ans += w[s-(d3+1)*c3-(d4+1)*c4];
if (s-(d1+1)*c1-(d2+1)*c2-(d3+1)*c3 >= 0) ans -= w[s-(d1+1)*c1-(d2+1)*c2-(d3+1)*c3];
if (s-(d2+1)*c2-(d3+1)*c3-(d4+1)*c4 >= 0) ans -= w[s-(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