#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define fi first #define se second #define pb push_back #define all(x) (x).begin(), (x).end() #define sz(x) (int((x).size())) #define bit(x) (1 << (x)) #define cnt1(x) (__builtin_popcount(x)) template inline void chkmax(T& x, U y) { if (x < y) x = y; } template inline void chkmin(T& x, U y) { if (y < x) x = y; } typedef long long LL; typedef double DB; typedef pair PII; typedef vector VI; int n, m; const int MB = 14; int dp[MB + 1][4]; int s[MB + 1], t[MB + 1]; int A[MB + 1], B[MB + 1]; int calc(int a, int b) { for (int i = 1; i <= MB; i++) { A[i - 1] = !!(a & bit(MB - i)); B[i - 1] = !!(b & bit(MB - i)); } memset(dp, 0, sizeof dp); dp[0][0] = 1; for (int i = 0; i < MB; i++) { for (int st = 0; st < 4; st++) { int x = dp[i][st]; if (!x) continue; if (A[i] == B[i]) { int nst = st; if (!A[i]) { if (s[i]) nst |= 1; if (t[i]) nst |= 2; dp[i + 1][nst] += x; } else { if (((st & 1) || s[i]) && ((st & 2) || t[i])) dp[i + 1][nst] += x; } } else { if ((st & 1) || s[i]) { int nst = st; if (t[i]) nst |= 2; dp[i + 1][nst] += x; } if ((st & 2) || t[i]) { int nst = st; if (s[i]) nst |= 1; dp[i + 1][nst] += x; } } } } int ans = 0; for (int i = 0; i < 4; i++) ans += dp[MB][i]; return ans; } int main() { // freopen("in.txt", "r", stdin); // freopen("out.txt", "w", stdout); int tc; for (scanf("%d", &tc); tc--; ) { scanf("%d%d", &n, &m); LL ans = 0; for (int i = 1; i <= MB; i++) { s[i - 1] = !!(n & bit(MB - i)); t[i - 1] = !!(m & bit(MB - i)); } int x = max(n, m), k = 1; while (k <= x) k *= 2; for (int a = 1; a < k; a++) { for (int b = a; ; b = (a & (b - 1))) { ans += __gcd(a, b) * calc(a, b); if (b == 0) break; } } for (int a = 1; a <= n; a++) ans -= a; for (int b = 1; b <= m; b++) ans -= b; printf("%lld\n", ans); } return 0; }