#include <bits/stdc++.h>
using namespace std;

#define endl     '\n'
#define int      long long

const int N = 2e5, oo = 2e18, MOD = 998244353;

int xorfromZeroToN(int n) {
    if (n % 4 == 0) return n;
    return n ^ xorfromZeroToN(n-1);
}

int xRange(int l, int r) {
    if (l == 0)
        return xorfromZeroToN(r);
    return xorfromZeroToN(r) ^ xorfromZeroToN(l-1);
}

int cnt(int n) {
    if (n < 0)
        return 0;
    if (n == 0)
        return 1; 
    int ans = (n >= 3) + 1;
    if (n >= 3)
        ans += ((n - 3) / 4);
    return ans;
}
int cnt2(int n) {
    if (n <= 0)
        return 0;
    int ans = 1;
    if (n >= 1)
        ans += (n - 1) / 4;
    return ans;
}

int ans(int l, int r) {
    if (l > r)
        return 0;
    int c = cnt(r) - cnt(l-1);
    int c2 = cnt2(r) - cnt2(l-1);
    // cout << c  << ' ' << c2 << endl;
    return c * (c - 1) / 2 + c2 * (c2 - 1) / 2;
}
// 0 1 2 0

void solve() {
    int n, x; cin >> n >> x;
    // cout << cnt(5) << ' ' << cnt2(5) << endl;
    // cout << cnt(0) << ' ' << cnt2(0) << endl;
    // cout << cnt(4) << ' ' << cnt2(4) << endl;
    // 0 1 3 0 4 1
    // cout << cnt(7) << ' ' << cnt2(7) << endl;
    // cout << cnt(0) << ' ' << cnt2(0) << endl;
    // cout << ans(1, x - 1) << endl;
    // cout << ans(x + 1, n) << endl;
    int res = ans(0, n) % MOD;
    // cout << ans(0, n) << ' ' << ans(2, n) << ' ' << ans(0, 0) << endl;
    res = (res - ans(x, n) % MOD + MOD) % MOD;
    res = ((res - ans(0, x - 1) % MOD) + MOD) % MOD;
    cout << res << endl;
    // int ans = 0;
    // for (int l = 1; l <= x; l++) {
    //     for (int r = x; r <= n; r++) {
    //         if (xRange(l, r) == 0) {
    //             cout << l << ' ' << r << endl;
    //         }
    //     }
    // }
    // for (int i = 0; i <= 8; i++) {
    //     cout << i << ' ' << xorfromZeroToN(i) << endl;
    // }
}


signed main() {
    ios_base::sync_with_stdio(false); 
    cin.tie(NULL); cout.tie(NULL);
    // #ifndef ONLINE_JUDGE 
    //    freopen("input.txt", "r", stdin); 
    //    freopen("output.txt", "w", stdout); 
    // #endif 
    int t; t = 1;
    cin >> t;
    while (t--) solve();
    return 0;
}
