#include #include #include #include #define LL long long using namespace std; const LL Maxn = 510; const LL Mod = 1e9+7; LL n; LL gcd(LL A, LL B) { return A == 0 ? B : gcd(B%A, A); } LL qpow(LL x, LL k) { LL ret = 1; while(k){ if(k&1) ret = ret*x%Mod; x = x*x%Mod; k >>= 1; } return ret; } LL l[5], now[5], num[5]; int main() { LL i, j, k; LL T; scanf("%I64d", &T); while(T--){ for(i = 1; i <= 4; i++) scanf("%I64d", &l[i]); for(i = 1; i <= 4; i++){ num[i] = 1; now[i] = l[i-1]+1; } scanf("%I64d", &n); n--; LL ans = 0; while(n > 0){ LL mn = 10000000000ll, x; for(i = 1; i <= 4; i++){ if(now[i]*(4-i+1) < mn) x = i, mn = now[i]*(4-i+1); } if(num[x]*4 >= n){ ans += n*mn; break; } ans += num[x]*4*mn; n -= num[x]*4; if(now[x] == 2*l[x]){ now[x] = 10000000000ll; continue; } num[x]++; if(now[x] <= 2*l[x-1]) num[x]++; if(now[x] >= l[x]) num[x]--; if(now[x] >= l[x]+1) num[x]--; now[x]++; } printf("%I64d\n", ans); } return 0; }