I=input P=998244353 u=(P+1)//2 for t in range(int(I())): N,M,L,R=map(int,I().split()) N-=1 a,b,c,d=R//N,-((M-L)//N),-((M-R)//N),L//N print((M*(c-b)+R*(a-c+1)+((d+b)*(d-b+1)-(a+c)*(a-c+1))*u*N-L*(d-b+1)+a-b+1)%P)