#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/(x+3LL))*(x/2LL+2LL); if (n%(x+3LL) < x) { ans += (n%(x+3LL)+1LL)/2LL; } else { ans += 1LL+(n%(x+3LL)-x)/2LL; ans += x/2LL; } printf("%lld\n", ans%mod_num); } t--; } return 0; }