import sys input = lambda :sys.stdin.readline()[:-1] ni = lambda :int(input()) na = lambda :list(map(int,input().split())) yes = lambda :print("yes");Yes = lambda :print("Yes");YES = lambda : print("YES") no = lambda :print("no");No = lambda :print("No");NO = lambda : print("NO") ####################################################################### def f(a, b, l, r): F = lambda x:a * (x + 1) * x //2 + b * x return F(a) - F(b) mod = 998244353 for _ in range(ni()): n,m,l,r = na() ans = (r-l+1) # d = [1, (m-r)//(n-1)] ans += (m-r)//(n-1) * (r - l + 1) # d = [(m-r)/(n-1)+1, m//(n-1)] ans += f(-(n-1), m, (m-r)//(n-1), m//(n-1)) - (m//(n-1)-(m-r)//(n-1)) * (l) ans += l // (n-1) * (r - l + 1) ans += (r//(n-1) - l//(n-1)) * (r+1)-f(-(n-1), m, l//(n-1), r//(n-1)) print(ans%mod) #