/*
f(a, b, c, n) = \sum_{i = 0}^n\lfloor\frac{ai + b}{c}\rfloor
$O(\log n)$
*/
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
// 注意以下计算 x 取值是 (0, n],即不包括 0
// 特别注意的是这里的 a, b, n 不能提前模(有向下取整)!
template <class Info>
struct floorSum {
LL a, b, c, n;
Info u, r;
floorSum(LL a, LL b, LL c, LL n, Info u, Info r) : a(a), b(b), c(c), n(n), u(u), r(r) {}
Info qpow(Info a, LL b) {
Info res = 1;
while (b) {
if (b & 1) {
res = res + a;
}
b >>= 1;
a = a + a;
}
return res;
}
Info operator()() {
if (n <= 0) {
return Info();
}
return qpow(u, b / c) + solve(a, b % c, c, n, u, r);
}
LL calc(const LL &a, const LL &b, const LL &c, const LL &x) { return (__int128(a) * x + b) / c; }
Info solve(LL a, LL b, LL c, LL n, Info u, Info r) {
if (n == 0) {
return Info();
}
if (a >= c) {
return solve(a % c, b, c, n, u, qpow(u, a / c) + r);
}
LL m = calc(a, b, c, n);
if (m == 0) {
return qpow(r, n);
}
LL cnt = n - calc(m, -b - 1, a, c);
return qpow(r, (c - b - 1) / a) + u + solve(c, (c - b - 1) % a, a, m - 1, r, u) + qpow(r, cnt);
}
};
constexpr LL MOD = 998244353;
struct Info {
LL cntu, cntr, sumi, sums, sqrs, prod;
Info() : cntu(0), cntr(0), sumi(0), sums(0), sqrs(0), prod(0) {}
};
Info operator+(const Info &a, const Info &b) {
Info c;
c.cntu = (a.cntu + b.cntu) % MOD, c.cntr = (a.cntr + b.cntr) % MOD;
c.sumi = (a.sumi + b.sumi + a.cntr * b.cntr) % MOD;
c.sums = (a.sums + b.sums + a.cntu * b.cntr) % MOD;
c.sqrs = (a.sqrs + b.sqrs + ((a.cntu * a.cntu) % MOD) * b.cntr + (2 * a.cntu * b.sums) % MOD) % MOD;
c.prod = ((a.prod + b.prod + ((a.cntu * a.cntr) % MOD) * b.cntr) % MOD + a.cntu * b.sumi + a.cntr * b.sums) % MOD;
return c;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int T;
cin >> T;
while (T--) {
LL a, b, c, n;
cin >> n >> a >> b >> c;
Info u, r;
u.cntu = 1;
r.cntr = r.sumi = 1;
auto x = floorSum<Info>(a, b, c, n, u, r)();
cout << (x.sums + b / c) % MOD << ' ' << (x.sqrs + (b / c) * (b / c) % MOD) % MOD << ' ' << x.prod << "\n";
}
return 0;
}