#include int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); int t; std::cin >> t; while (t--) { int len[5]; for (int i = 1; i <= 4; ++i) std::cin >> len[i]; auto getp = [&](__int128 x, __int128 m) { __int128 cnt, sum; if (m >= 2 * x) { cnt = (2 * x + 1) * (2 * x + 1); sum = 2 * x * (2 * x + 1) * (x + 1); } else if (m <= x) { cnt = 2 * m * m + 2 * m + 1; sum = 2 * m * (2 * m + 1) * (m + 1) / 3; } else { cnt = (2 * x + 1) * (2 * x + 1) - 2 * (m - 2 * x - 1) * (m - 2 * x); sum = 2 * x * (2 * x + 1) * (x + 1) - (m - 2 * x - 1) * (m - 2 * x) * (m + x + 1) * 4 / 3; } return std::make_pair(cnt, sum); }; auto get = [&](__int128 m) { __int128 cnt = 0, sum = 0; for (int i = 1; i <= 4; ++i) { auto p = getp(len[i], m / (5 - i)); if (i > 1) { auto p1 = getp(len[i - 1], m / (5 - i)); p.first -= p1.first; p.second -= p1.second; } cnt += p.first; sum += p.second * (5 - i); } return std::make_pair(cnt, sum); }; int64_t k; std::cin >> k; if (k == 0) { std::cout << 0 << "\n"; continue; } __int128 l = 0, r = 1e10; while (l < r) { __int128 m = (l + r + 1) / 2; auto p = get(m).first; if (p <= k) { l = m; } else { r = m - 1; } } auto p = get(l); std::cout << int64_t(p.second + (k - p.first) * (l + 1)) << "\n"; } return 0; }