def f(x, N): if x == -1: return 0 q = x // (N - 1) r = x % (N - 1) return (N - 1) * q * (q - 1) // 2 + q * (r + 1) def solve(N, M, L, R): return f(R, N) - f(L - 1, N) + f(M - L, N) - f(M - R - 1, N) + R - L + 1 T = int(input()) for i in range(T): N, M, L, R = map(int, input().split()) print(solve(N, M, L, R) % 998244353)