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

void solve() {
    ll x, m;
    cin >> x >> m;
    
    ll ans = 0;
    
    // 計算小於 x 的情況
    for(ll y = 1; y <= min(x,m); y++) {
        ll xor_val = x ^ y;
        if(xor_val % y == 0 || xor_val % x == 0) {
            ans++;
        }
    }
    
    // 計算大於 x 的情況
    // 當 y = k*x 時，x ^ y = x ^ (k*x)
    // 這種情況下，我們可以直接計算有多少個 k 符合條件
    if(x > 0) {
        ll k = (m + x - 1) / x;  // 向上取整
        if(k > 1) {
            ans += k - 1;
        }
    }
    
    cout << ans << "\n";
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int t;
    cin >> t;
    while(t--) {
        solve();
    }
    
    return 0;
}