#include #include #include #include #include #include #include #include #include #include #define pb push_back #define fr first #define sc second #define mp make_pair #define pii pair #define vi vector typedef long long ll; void gn(int &x) { x = 0; int f = 1; char ch = getchar(); while (ch < '0' || ch > '9'){ if (ch == '-') f = -1; ch = getchar(); }while (ch >= '0' && ch <= '9'){ x = x * 10 + ch - '0'; ch = getchar(); }x *= f; } /*----------------------------------------------------------------*/ #define maxn 100010 using namespace std; int T, n, m, l, r; int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } int bit[20]; int c[20], b[20], d[20], tot, sum; int main() { gn(T); bit[0] = 1; for (int i = 1; i <= 15; ++ i) bit[i] = 2 * bit[i - 1]; while (T --){ ll ans = 0, cnt = 0; gn(n); gn(m); if (n < m) swap(n, m); int up = 1; while (up <= n) up *= 2; for (int t = 1; t < up; ++ t){ for (int s = t; ; s = (s - 1) & t){ if (s == t && s <= m && s <= n){ ans += s; continue; } sum = t - s; l = max(sum + s - m, max(1 - s, 0)); r = min(n - s, min(sum + s - 1, sum)); if (l > r){ if (! s) break; else continue; } tot = 0; int d = gcd(t, s); for (int i = 0; i < 15; ++ i) if (bit[i] & sum) c[++ tot] = bit[i]; int now = 0, lt = 0, rt = 0; for (int i = tot; i >= 1; -- i){ if (l - 1 >= now + c[i]) lt += bit[i - 1], now += c[i]; } if (l == 0) lt = -1; now = 0; for (int i = tot; i >= 1; -- i){ if (r >= now + c[i]) rt += bit[i - 1], now += c[i]; } ans += 1ll * (rt - lt) * d; if (! s) break; } } printf("%I64d\n", ans); } }