#include #define rep(i, n) for(int i=0, i##_len=(n); i=0; --i) #define rreps(i, n) for(int i=((int)(n)); i>0; --i) #define all(v) (v).begin(), (v).end() #define el '\n' using namespace std; using ll = long long; using ull = unsigned long long; using vi = vector; using vvi = vector>; using vvvi = vector>>; using vl = vector; using vvl = vector>; using vvvl = vector>>; using vs = vector; using pi = pair; using pl = pair; template using min_priority_queue = priority_queue, greater>; template bool chmax(T &a, const T &b) { if (a bool chmin(T &a, const T &b) { if (b bool chmaxeq(T &a, const T &b) { if (a<=b) { a=b; return 1; } return 0; } template bool chmineq(T &a, const T &b) { if (b<=a) { a=b; return 1; } return 0; } bool yes(bool a=true) { cout << (a?"yes":"no") << el; return a; } bool no(bool a=true) { cout << (a?"no":"yes") << el; return a; } bool Yes(bool a=true) { cout << (a?"Yes":"No") << el; return a; } bool No(bool a=true) { cout << (a?"No":"Yes") << el; return a; } bool YES(bool a=true) { cout << (a?"YES":"NO") << el; return a; } bool NO(bool a=true) { cout << (a?"NO":"YES") << el; return a; } template istream &operator>>(istream &is, pair &p); template ostream &operator<<(ostream &os, const pair &p); template istream &operator>>(istream &is, vector &v); template ostream &operator<<(ostream &os, const vector &v); template istream &operator>>(istream &is, pair &p) { return is >> p.first >> p.second; } template ostream &operator<<(ostream &os, const pair &p) { return os << p.first << ' ' << p.second; } template istream &operator>>(istream &is, vector &v) { int sz = v.size(); for (int i = 0; i < sz; i++) is >> v[i]; return is; } template ostream &operator<<(ostream &os, const vector &v) { int sz = v.size(); for (int i = 0; i < sz; i++) { if (i) os << ' '; os << v[i]; } return os; } void _main(); int main() { cin.tie(0); ios::sync_with_stdio(0); cout << fixed << setprecision(16); _main(); return 0; } template struct static_mint { using mint = static_mint; static_mint(long long x = 0) : _x((x%mod+mod)%mod) {} long long val() const { return _x; } mint operator+() const { return mint(_x); } mint operator-() const { return mint(-_x); } mint &operator+=(const mint a) { if ((_x += a._x) >= mod) _x -= mod; return *this; } mint &operator-=(const mint a) { if ((_x += mod - a._x) >= mod) _x -= mod; return *this; } mint &operator*=(const mint a) { (_x *= a._x) %= mod; return *this; } mint &operator++() { *this += 1; return *this; } mint operator++(int) { mint temp = *this; ++*this; return temp; } mint &operator--() { *this -= 1; return *this; } mint operator--(int) { mint temp = *this; --*this; return temp; } mint operator+(const mint a) const { return mint(*this) += a; } mint operator-(const mint a) const { return mint(*this) -= a; } mint operator*(const mint a) const { return mint(*this) *= a; } friend mint operator+(const long long a, const mint b) { return mint(a) + b; } friend mint operator-(const long long a, const mint b) { return mint(a) - b; } friend mint operator*(const long long a, const mint b) { return mint(a) * b; } mint pow(long long t) const { if (t < 0) return pow(-t).inv(); if (t == 0) return 1; mint a = pow(t >> 1); a *= a; if (t & 1) a *= *this; return a; } mint inv() const { return pow(mod - 2); } mint &operator/=(const mint a) { return *this *= a.inv(); } mint operator/(const mint a) const { return mint(*this) /= a; } friend mint operator/(const long long a, const mint b) { return mint(a) / b; } bool operator==(const mint a) const { return _x == a._x; } bool operator!=(const mint a) const { return _x != a._x; } friend istream &operator>>(istream &is, mint &a) { long long x; is >> x; a = mint(x); return is; } friend ostream &operator<<(ostream &os, const mint &a) { os << a.val(); return os; } private: long long _x; }; // using mint = static_mint<1000000007>; // using mint = static_mint<998244353>; using mint = static_mint<998244353>; void _main() { ll T; cin >> T; rep(_, T) { ll N, A; cin >> N >> A; if (A == 1) { cout << mint(N)*(N-1)/2 << el; continue; } mint ans = 0; ll p = N; ll x = 0; while (p) { ll np = p/A; mint d = p-np; ans += d*(d-1)/2+d*x; x += p-np*A+1; p = np; } cout << ans << el; } }