import sys input = sys.stdin.readline mod = 998244353 T = int(input()) for _ in range(T): N, M, L, R = map(int, input().split()) ans = 0 # d=0 ans += R-L+1 # d>0 d1 = (L+N-2)//(N-1) d2 = R//(N-1) ans += (R-L+1)*(d1-1)+(R-(N-1)*d1+1 + R-(N-1)*d2+1)*(d2-d1+1)//2 # d<0 d1 = (M-R+N-2)//(N-1) d2 = (M-L+1)//(N-1) ans += (R-L+1)*(d1-1)+(M-L-(N-1)*d1+1 + M-L-(N-1)*d2+1)*(d2-d1+1)//2 print(ans % mod)