#include int main () { int t = 0; long long n = 0LL; long long x = 0LL; int res = 0; long long mod_num = 998244353LL; res = scanf("%d", &t); while (t > 0) { res = scanf("%lld", &n); res = scanf("%lld", &x); if (x%2LL == 1LL) { printf("%lld\n", ((n+1LL)/2LL)%mod_num); } else { long long ans = (n/(2LL*x+1LL))*(x+1LL); if (n%(2LL*x+1LL) < x) { ans += (n%(2LL*x+1LL)+1LL)/2LL; } else { ans += 1LL+(n%(2LL*x+1LL)-x)/2LL; ans += x/2LL; } printf("%lld\n", ans%mod_num); } t--; } return 0; }