#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;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CnVzaW5nIGxsID0gbG9uZyBsb25nOwoKdm9pZCBzb2x2ZSgpIHsKICAgIGxsIHgsIG07CiAgICBjaW4gPj4geCA+PiBtOwogICAgCiAgICBsbCBhbnMgPSAwOwogICAgCiAgICAvLyDoqIjnrpflsI/mlrwgeCDnmoTmg4Xms4EKICAgIGZvcihsbCB5ID0gMTsgeSA8PSBtaW4oeCxtKTsgeSsrKSB7CiAgICAgICAgbGwgeG9yX3ZhbCA9IHggXiB5OwogICAgICAgIGlmKHhvcl92YWwgJSB5ID09IDAgfHwgeG9yX3ZhbCAlIHggPT0gMCkgewogICAgICAgICAgICBhbnMrKzsKICAgICAgICB9CiAgICB9CiAgICAKICAgIC8vIOioiOeul+Wkp+aWvCB4IOeahOaDheazgQogICAgLy8g55W2IHkgPSBrKngg5pmC77yMeCBeIHkgPSB4IF4gKGsqeCkKICAgIC8vIOmAmeeoruaDheazgeS4i++8jOaIkeWAkeWPr+S7peebtOaOpeioiOeul+acieWkmuWwkeWAiyBrIOespuWQiOaineS7tgogICAgaWYoeCA+IDApIHsKICAgICAgICBsbCBrID0gKG0gKyB4IC0gMSkgLyB4OyAgLy8g5ZCR5LiK5Y+W5pW0CiAgICAgICAgaWYoayA+IDEpIHsKICAgICAgICAgICAgYW5zICs9IGsgLSAxOwogICAgICAgIH0KICAgIH0KICAgIAogICAgY291dCA8PCBhbnMgPDwgIlxuIjsKfQoKaW50IG1haW4oKSB7CiAgICBpb3M6OnN5bmNfd2l0aF9zdGRpbyhmYWxzZSk7CiAgICBjaW4udGllKG51bGxwdHIpOwogICAgCiAgICBpbnQgdDsKICAgIGNpbiA+PiB0OwogICAgd2hpbGUodC0tKSB7CiAgICAgICAgc29sdmUoKTsKICAgIH0KICAgIAogICAgcmV0dXJuIDA7Cn0=