MOD = 998244353 def calc(n, X, Y): x = -(-X // n) * n y = Y // n * n if x > y: return (Y - X + 1) * Y // n else: tmp = (x - X) * x // n tmp += (Y - y + 1) * y // n tmp += n * (y - x) * (x + y - 1) // 2 return tmp % MOD def solve(): N, M, L, R = map(int, input().split()) N1 = N - 1 ans = R - L + 1 ans += calc(N - 1, L, R) ans += calc(N - 1, M - R, M - L) ans %= MOD print(ans) T = int(input()) for t in range(T): solve()