#include using namespace std; #define rep(i,n) for(int i = 0; i < (int)(n); ++i) #define rrep(i,n) for(int i = (int)(n) - 1; i >= 0; --i) #define ALL(a) a.begin(), a.end() #define Sort(a) sort(a.begin(), a.end()) #define RSort(a) sort(a.rbegin(), a.rend()) typedef long long int ll; typedef long double ld; typedef vector vi; typedef vector vll; typedef vector vc; typedef vector vst; typedef vector vd; typedef pair P; const long long INF = 0x1fffffffffffffff; const long long MOD = 998244353; const long double PI = acos(-1); template inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } template inline T vin(T& vec, U n) { vec.resize(n); for(int i = 0; i < (int) n; ++i) cin >> vec[i]; return vec; } template inline void vout(T vec, string s = "\n"){ for(auto x : vec) cout << x << s; } template void in(T&... a){ (cin >> ... >> a); } void out(){ cout << '\n'; } template void out(const T& a, const Ts&... b){ cout << a; (cout << ... << (cout << ' ', b)); cout << '\n'; } template void inGraph(vector>& G, U n, U m, bool directed = false){ G.resize(n); for(int i = 0; i < m; i++){ int a, b; cin >> a >> b; a--, b--; G[a].push_back(b); if(!directed) G[b].push_back(a); } } ll t; void input(){ in(t); } ll solve2(ll n, ll x){ ll ans = 0; if(n < x){ ans = n / 2 + n % 2; ans %= MOD; return ans; } // x <= n vll dp(n + 1); vll check(n + 1); for(int i = 1; i <= n; i++){ if(i < x){ if(i % 2){ dp[i] = 1; }else{ dp[i] = 0; } }else{ if(dp[i - 1] == 0){ dp[i] = 1; continue; } if(dp[i - x] == 0 && check[i - x] == 0){ dp[i] = 1; check[x] = 1; continue; } } } for(int i = 1; i <= n; i++){ if(dp[i]){ ans++; } } return ans; } void solve(){ ll n, x; in(n, x); ll ans = 0; if(n < x){ ans = n / 2 + n % 2; ans %= MOD; out(ans); return; } // x <= n if(x % 2 == 1){ ans = n / 2 + n % 2; }else{ ans = (x - 1) / 2 + (x - 1) % 2; n -= x - 1; ll d = n / (x + 1), m = n % (x + 1); ans += d * (x + 2) / 2; ans += m / 2 + m % 2; } ans %= MOD; out(ans); } int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); input(); while(t--) solve(); }