#include <bits/stdc++.h>
using namespace std;
const int S = 1e5+5;
typedef long long ll;
int c[5], tot, d[5], s;
ll dp[S];
int main () {
freopen("coin.in", "r", stdin);
freopen("coin.out", "w", stdout);
for (int i = 1;i <= 4;i++) cin >> c[i];
cin >> tot;
dp[0] = 1;
for (int i = 1;i <= 4;i++) {
for (int j = c[i];j <= 1e5;j++) dp[j] += dp[j-c[i]];
}
while (tot--) {
for (int i = 1;i <= 4;i++) cin >> d[i]; cin >> s;
ll ans = 0;
for (int i = 0;i < (1<<4);i++) {
int op = 1, j = i; ll k = 0;
for (int z = 0;z < 4;z++, j >>= 1) {
if (j&1) {
op ^= 1;
k += (d[z+1]+1)*c[z+1];
}
}
if (k > s) continue;
if (op) ans += dp[s-k];
else ans -= dp[s-k];
}
cout << ans << endl;
}
return 0;
}