import sys readline=sys.stdin.readline def floor_sum(n: int, m: int, a: int, b: int) -> int: assert 0 <= n assert 1 <= m ans = 0 if a >= m: ans += (n - 1) * n * (a // m) // 2 a %= m if b >= m: ans += n * (b // m) b %= m y_max = (a * n + b) // m x_max = y_max * m - b if y_max == 0: return ans ans += (n - (x_max + a - 1) // a) * y_max ans += floor_sum(y_max, a, m, (a - x_max % a) % a) return ans T=int(readline()) for t in range(T): N,M,L,R=map(int,readline().split()) mod=998244353 M+=1 R+=1 def solve(A,B): a,aa=divmod(A,N-1) b,bb=divmod(B,N-1) retu=(a+1)*(b+1)*min(aa,bb) if aabb: retu+=(a+1)*b*(aa-bb) retu+=a*b*(N-1-max(aa,bb)) return retu ans=solve(M,R)-solve(M,L) ans%=mod print(ans)