#include #include #include #include #include #include #include #include #include #include #include #include #include #include #define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl; #define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl; 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; } using namespace std; typedef long long ll; template vector> vec2d(int n, int m, T v){ return vector>(n, vector(m, v)); } template vector>> vec3d(int n, int m, int k, T v){ return vector>>(n, vector>(m, vector(k, v))); } template void print_vector(vector v, char delimiter=' '){ if(v.empty()) { cout << endl; return; } for(int i = 0; i+1 < v.size(); i++) cout << v[i] << delimiter; cout << v.back() << endl; } const ll MOD = 998244353; using mint = atcoder::modint998244353; ostream& operator<<(ostream& os, const mint& m){ os << m.val(); return os; } #define N_MAX 200002 mint inv[N_MAX],fac[N_MAX],finv[N_MAX]; void init(){ fac[0]=1;fac[1]=1; finv[0]=1;finv[1]=1; inv[1]=1; for(int i=2;i= 1){ int j_nx = k, k_nx = 0; if(!dp[i-1][j_nx][k_nx]) dp[i][j][k] = true; } if(i >= x && j == 0){ int j_nx = k, k_nx = 1; if(!dp[i-x][j_nx][k_nx]) dp[i][j][k] = true; } } } if(dp[i][0][0]) { // cout << i << endl; ans++; } } return ans; } const ll inf = 2e18; ll solve(ll n, ll x){ if(x%2 == 1){ return (n+1)/2; } ll m = x/2; auto kth = [&](ll k){ if(k <= m) return 2*k-1; ll base = m*2-1; ll rem = k-m; ll cycle = rem/(m+2); ll tail = rem%(m+2); ll ans = base + cycle*(2*m+3); if(tail >= 1){ ans += 1+2*(tail-1); } return ans; }; ll l = 0, r = inf; while(r-l > 1){ ll k = (l+r)/2; if(kth(k) > n) r = k; else l = k; } // debug_value(kth(10)) return l; } void test(int n){ for(int x = 1; x <= 2*n; x++){ if(solve(n, x) != naive(n, x)){ cout << n << ' ' << x << ' ' << solve(n, x) << ' ' << naive(n, x) << endl; } } } int main(){ ios::sync_with_stdio(false); cin.tie(0); cout << setprecision(10) << fixed; // naive(100, 8); // int n; cin >> n; // test(n); // return 0; int t; cin >> t; while(t--) { ll n, x; cin >> n >> x; cout << mint(solve(n, x)) << endl; } }