#pragma GCC optimize("Ofast") #include using namespace std; typedef long long int ll; mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); ll myrand(ll B){ return (unsigned long long) rng() % B; } constexpr ll mod = 998244353; ll mod_pow(ll a,ll b){ a%=mod; if(b==0)return 1; if(b==1)return a; ll res=mod_pow(a,b/2)%mod; res*=res; res%=mod; if(b%2)res*=a; return res%mod; } void add(int &x,int y){ x += y; if(x >= mod) x -= mod; } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); // for(int i=0;i<10;i++){ // int n = myrand(30)+1, x = myrand(n)+1; // map>,bool> mp; // auto slv = [&](auto self,int cur,int a,int b)->bool{ // if(mp.find({cur,{a,b}}) != mp.end()){ // return mp[{cur,{a,b}}]; // } // bool res = false; // if(cur == 0) return false; // if(!self(self, cur-1, b, 0)){ // res = true; // } // if(cur >= x and !a and !self(self, cur-x, b, 1)){ // res = true; // } // mp[{cur,{a,b}}] = res; // return res; // }; // cout << n << " " << x << endl; // for(int i=1;i<=n;i++){ // cout << "i = " << i << " " << slv(slv,i,0,0) << endl; // } // } int q; cin >> q; while(q--){ ll n,x; cin >> n >> x; ll res = (n+1)/2; ll ad = n/x - n/(2*x); if(x%2 == 0) res += ad; cout << res%mod << "\n"; } }