using System; using static System.Console; using System.Linq; using System.Collections.Generic; class Program { static int NN => int.Parse(ReadLine()); static long[] NList => ReadLine().Split().Select(long.Parse).ToArray(); public static void Main() { Solve(); } static void Solve() { var t = NN; var ans = new long[t]; for (var u = 0; u < t; ++u) { var c = NList; var (n, m, l, r) = ((int)c[0], c[1], c[2], c[3]); ans[u] = Incline(n, m, l, r); } WriteLine(string.Join("\n", ans)); } static readonly int mod = 998_244_353; static long Incline(int n, long m, long l, long r) { var ans = F(n, m, r); if (l > 0) ans = (ans + mod - F(n, m, l - 1)) % mod; return ans; } static long F(int n, long m, long r) { return (m / (n - 1) % mod * (r % mod + 1) % mod + G(n, m, r)) % mod; } static long G(int n, long m, long r) { return ((r ) / (n - 1) % mod * (m % (n - 1) + 1) % mod + Math.Min(r % (n - 1), m % (n - 1)) % mod + 1) % mod; } }