import sys input = sys.stdin.readline mod = 998244353 for _ in range(int(input())): n, m, l, r = map(int, input().split()) ans = (r - l + 1) % mod ans += (r - l + 1) * (m // (n - 1)) % mod ans %= mod if m > 0 and n > 2 and m % (n - 1) == 0: ans += (r // (n - 1) - (l - 1) // (n - 1)) % mod ans %= mod print(ans)