#include <bits/stdc++.h>
#include <atcoder/modint>
using namespace std;
using mint = atcoder::modint998244353;
void solve() {
  long long N, M, L, R;
  cin >> N >> M >> L >> R;
  mint ret = 0;
  ret += mint(L / (N - 1) + 1) * (R - L + 1); // (0 <= k <= L / (N - 1))
  // L / (N - 1) + 1 <= k <= R / (N - 1)
  ret += mint(R - L + 1) * (R / (N - 1) - L / (N - 1));
  if(R - (L / (N - 1) + 1) * (N - 1) >= 0) {
    mint r1 = (L / (N - 1) + 1) * (N - 1) - L;
    mint d1 = R / (N - 1) - L / (N - 1);
    ret -= r1 * d1;
    ret -= d1 * (d1 - 1) / 2 * (N - 1);
  }
  ret += mint((M - R) / (N - 1)) * (R - L + 1); // 1 <= k <= (M - R) / (N - 1)
  ret += mint(R - L + 1) * ((M - L) / (N - 1) - (M - R) / (N - 1));
  if(((M - R) / (N - 1) + 1) * (N - 1) + L <= M) {
    mint r2 = R + ((M - R) / (N - 1) + 1) * (N - 1) - M; 
    mint d2 = (M - L) / (N - 1) - (M - R) / (N - 1);
    ret -= r2 * d2;
    ret -= d2 * (d2 - 1) / 2 * (N - 1);
  }
  cout << ret.val() << '\n';
}
int main() {
  cin.tie(0); cout.tie(0);
  ios::sync_with_stdio(false);
  int T;
  cin >> T;
  while(T--) {
    solve();
  }
  return 0;
}