#include #include using namespace std; using ll = long long; #define REP(i,n) for(int i=0;i void chmin(T& a, T b) { a = min(a, b); } template void chmax(T& a, T b) { a = max(a, b); } #define PR(x) cerr << #x << "=" << x << endl using i128 = __int128_t; int main() { int t; cin >> t; while(t--) { ll n, m ,l ,r; cin >> n >> m >> l >> r; mint sum = 0; sum += (r / (n-1) - (l+n-2) / (n-1) + 1) * (n-1); sum += (r % (n-1)) - (l+n-2) % (n-1); mint ans = 0; ans += sum * ((m+1) / (n-1)); // PR(ans.val()); ans += (r / (n-1) - (l+n-2) / (n-1) + 1) * ((m+1) % (n-1)); // PR(ans.val()); ans += min(r % (n-1)+1, (m+1) % (n-1)) - min((l+n-2) % (n-1)+1, (m+1) % (n-1)); // PR(ans.val()); cout << ans.val() << endl; } return 0; }