#include #include using namespace std; int tn; long long n, m, x0, x2, xl, xr; long long b_at_left (long long x) { return x / 2; } long long b_at_right (long long x) { return x / 2; } int main(int argc, const char * argv[]) { cin >> tn; while (tn--) { cin >> n >> m >> x0 >> xl >> xr >> x2; if (m == 1) { cout << min(n * m, xl + xr + x0 + x2) << endl; continue; } long long new_pairs = 0; long long ans = xl + xr; long long can_put_bs = (xl % (m - 1) > 0) + xl / (m - 1); if (xr > 0) { long long rem_at_line = m - (xl + can_put_bs) % m; if (rem_at_line == m) can_put_bs += xr / (m - 1) + (xr % (m - 1) > 0); else if (rem_at_line < xr) can_put_bs += (xr - rem_at_line) / (m - 1) + ((xr - rem_at_line) % (m - 1) > 0); } // calc new_pairs long long complete_lines = xl / (m - 1); new_pairs += complete_lines * b_at_right(m - 1); long long remainder_for_l = xl - complete_lines * (m - 1); new_pairs += b_at_right(remainder_for_l); long long remain_space = min(xr, m - 1 - remainder_for_l); if (remain_space % 2 == 1 && remainder_for_l % 2 == 1) ++new_pairs; new_pairs += b_at_left(remain_space); complete_lines = (xr - remain_space) / (m - 1); new_pairs += b_at_left(m - 1) * complete_lines; new_pairs += b_at_left((xr - remain_space) - complete_lines * (m - 1)); can_put_bs = min(can_put_bs, x2); x2 -= can_put_bs; ans += can_put_bs; long long filled_lines = ans / m; if (ans > n * m) { cout << n * m << endl; continue; } long long holes = 0; if (ans % m != 0) { long long r = m - ans % m; if (r % 2 == 1) ++holes; can_put_bs = min(r / 2, x2); x2 -= can_put_bs; ans += can_put_bs; ++filled_lines; } if (m % 2 == 0) holes += n - filled_lines; can_put_bs = min((n - filled_lines) * (m / 2 + m % 2), x2); x2 -= can_put_bs; ans += can_put_bs; long long can_put_zs = min(n * m - ans, x0); ans += can_put_zs; ans += min(x2, min(holes / 2, new_pairs)); if (ans > n * 1LL * m) ans = n * 1LL * m; cout << ans << endl; } return 0; }