#include #include #include #include #include #include using namespace std; using i32 = int; using u32 = unsigned int; using i64 = long long; using u64 = unsigned long long; #define rep(i,n) for(int i=0; i<(int)(n); i++) const i64 INF = 1001001001001001001; using Modint = atcoder::static_modint<998244353>; template modint TriangleSumTimesLinear( long long n, long long a, long long b, modint c, modint d, modint e ){ if(n <= 0) return modint(0); if(a < b) return TriangleSumTimesLinear(n, b, a, d, c, e); long long maxi = (n - 1) / a + 1; modint offset = 0; offset += c * modint(maxi) * modint(maxi-1) * modint(maxi+1) / modint(6); offset += d * modint(maxi) * modint(maxi-1) * modint(maxi+1) / modint(6); offset += e * modint(maxi) * modint(maxi+1) / modint(2); return offset + TriangleSumTimesLinear(n-maxi*b, b, a-b, d, c-d, e+d*modint(maxi)); } template modint AKindOfFloorSum( long long n, long long m, long long a, long long b ){ long long q = b / m; modint tmp = modint(n) * modint(n-1) / 2 * q; b -= q * m; long long newn = a * (n-1) + b - (m-1); if(newn <= 0) return 0; return tmp + TriangleSumTimesLinear(newn, a, m, -1, 0, n-1); } template modint BKindOfFloorSum( long long n, long long m, long long a, long long b ){ long long q = b / m; modint tmp = modint(n) * modint(n-1) / 2 * q; b -= q * m; long long newn = a * (n-1) + b - (m-1); if(newn <= 0) return 0; return tmp + TriangleSumTimesLinear(newn, a, m, 0, 0, 1); } using Modint1 = atcoder::static_modint<1000000007>; using Modint2 = atcoder::static_modint<1000000009>; template modint find_mod( long long N, long long M, long long X, long long Y ){ modint x1 = AKindOfFloorSum(N, M, X, Y) * 2; modint x2 = BKindOfFloorSum(N, M, X, Y) * (1-N); modint x3 = AKindOfFloorSum(N, M, X, 0); modint x4 = BKindOfFloorSum(N, M, X, 0) * (-N); return x1 + x2 + x3 + x4; } int main(){ int T; cin >> T; rep(t,T){ long long N,M,X,Y; cin >> N >> M >> X >> Y; auto x1 = find_mod(N, M, X, Y); auto x2 = find_mod(N, M, X, Y); long long ans = atcoder::crt({ x1.val(), x2.val() }, { x1.mod(), x2.mod() }).first; cout << ans << '\n'; } return 0; } struct ios_do_not_sync{ ios_do_not_sync(){ ios::sync_with_stdio(false); cin.tie(nullptr); } } ios_do_not_sync_instance;