# https://atcoder.jp/contests/practice2/submissions/58141876 def floor_sum(n: int, m: int, a: int, b: int) -> int: res = 0 while 1: if a >= m or a < 0: res += n * (n - 1) * (a // m) // 2 a %= m if b >= m or b < 0: res += n * (b // m) b %= m y_max = a * n + b if y_max < m: break n, b = divmod(y_max, m) a, m = m, a return res MOD = 998244353 T = int(input()) for _ in range(T): N, M, L, R = map(int, input().split()) bottom = floor_sum(R-L+1, N-1, 1, L)%MOD top = floor_sum(R-L+1, N-1, 1, M-R)%MOD print((bottom+top+(R-L+1))%MOD)