#include using namespace std; #ifdef DEBUG #define display(x) cerr << #x << " = " << x << endl; #define eprintf(...) fprintf(stderr, __VA_ARGS__) #else #define display(x) {} #define eprintf(...) do {} while(0) #endif template bool chmin(T &a, const T &b) { return a > b ? a = b, true : false; } template bool chmax(T &a, const T &b) { return a < b ? a = b, true : false; } template ostream& operator << (ostream& out, const vector &v) { int n = v.size(); out << "{"; for(int i = 0; i < n; ++i) { if(i) out << ", "; out << v[i]; } return out << "}"; } template ostream& operator << (ostream& out, const pair &v) { return out << "(" << v.first << ", " << v.second << ")"; } typedef long long LL; typedef pair pii; const int maxN = 400000 + 5; int h[5][maxN + 5], tot[maxN + 5]; void report(int cnt, int val) { if(val >= maxN) return; for(int i = 3; i >= 0 && cnt > 0; --i) { int delta = min(h[i][val], cnt); cnt -= delta; h[i][val] -= delta; h[i + 1][val] += delta; } } void fill(int a) { for(int i = 1; i <= a; ++i) report(4 * i, 2 * a + 1 - i); for(int i = a; i >= 1; --i) report(4 * i, i); report(1, 0); } LL solve() { vector< vector > vp; vector len(4); for(int i = 0; i < 4; ++i) cin >> len[i], chmin(len[i], maxN); LL k; cin >> k; memset(h, 0, sizeof(h)); h[0][0] = 1; for(int x = 1; x < maxN; ++x) h[0][x] = x * 4; for(int i = 0; i < 4; ++i) fill(len[i]); memset(tot, 0, sizeof(tot)); for(int i = 1; i <= 4; ++i) for(int x = 0; x < maxN; ++x) if(i * x < maxN) tot[i * x] += h[i][x]; // display(vector(tot, tot + 10)); LL ans = 0; for(int i = 0; i < maxN && k; ++i) { LL delta = min(k, (LL)tot[i]); k -= delta; ans += delta * i; } return ans; } int main() { #ifndef LOCAL ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); #endif int T; cin >> T; while(T--) cout << solve() << endl; return 0; }