#include using namespace std; using ULL = __uint128_t; using LongLong = __int128_t; LongLong floor_sum(LongLong n, LongLong m, LongLong a, LongLong b) { auto safe_mod = [](LongLong x, LongLong m) -> LongLong { x %= m; if (x < 0) x += m; return x; }; auto floor_sum_unsigned = [](ULL n, ULL m, ULL a, ULL b) -> ULL { ULL ans = 0; while (true) { if (a >= m) { ans += n * (n - 1) / 2 * (a / m); a %= m; } if (b >= m) { ans += n * (b / m); b %= m; } ULL y_max = a * n + b; if (y_max < m) break; // y_max < m * (n + 1) // floor(y_max / m) <= n n = (ULL)(y_max / m); b = (ULL)(y_max % m); std::swap(m, a); } return ans; }; ULL ans = 0; if (a < 0) { ULL a2 = safe_mod(a, m); ans -= 1ULL * n * (n - 1) / 2 * ((a2 - a) / m); a = a2; } if (b < 0) { ULL b2 = safe_mod(b, m); ans -= 1ULL * n * ((b2 - b) / m); b = b2; } return ans + floor_sum_unsigned(n, m, a, b); } int solve() { long long N, M, L, R; cin >> N >> M >> L >> R; LongLong ret = R - L + 1; ret += floor_sum(R - L + 1, N - 1, 1, L); ret += floor_sum(R - L + 1, N - 1, 1, M - R); return ret % 998244353; } int main() { cin.tie(nullptr)->sync_with_stdio(false); int T; cin >> T; while (T--) cout << solve() << '\n'; }