#include <bits/stdc++.h>
using namespace std;
int tn;
int l, r;
long long ans = 0;
long long dp[32][2][2][2][2];
int getBit(int pos, int x) {
  if (x & (1 << pos)) {
    return 1;
  }
  return 0;
}
void solve() {
  scanf("%d %d", &l, &r);
  memset(dp, 0, sizeof(dp));
  dp[30][0][0][0][0] = 1;
  for (int i = 30; i > 0; i--) {
    for (int sa = 0; sa < 2; sa++) {
      for (int la = 0; la < 2; la++) {
        for (int sb = 0; sb < 2; sb++) {
          for (int lb = 0; lb < 2; lb++) {
            for (int nextA = 0; nextA < 2; nextA++) {
              for (int nextB = 0; nextB < 2; nextB++) {
                if (nextA == 1 && nextB == 1) {
                  continue;
                }
                if (getBit(i - 1, r) == 0) {
                  if (nextA == 1 && !sa) {
                    continue;
                  }
                  if (nextB == 1 && !sb) {
                    continue;
                  }
                }
                if (getBit(i - 1, l) == 1) {
                  if (nextA == 0 && !la) {
                    continue;
                  }
                  if (nextB == 0 && !lb) {
                    continue;
                  }
                }
                int goSa = sa, goLa = la, goSb = sb, goLb = lb;
                if (sa == false && getBit(i - 1, r) == 1 && nextA == 0) {
                  goSa = true;
                }
                if (la == false && getBit(i - 1, l) == 0 && nextA == 1) {
                  goLa = true;
                }
                if (sb == false && getBit(i - 1, r) == 1 && nextB == 0) {
                  goSb = true;
                }
                if (lb == false && getBit(i - 1, l) == 0 && nextB == 1) {
                  goLb = true;
                }
                dp[i - 1][goSa][goLa][goSb][goLb] += dp[i][sa][la][sb][lb];
              }
            }
          }
        }
      }
    }
  }
  long long ans = 0;
  for (int sa = 0; sa < 2; sa++) {
    for (int la = 0; la < 2; la++) {
      for (int sb = 0; sb < 2; sb++) {
        for (int lb = 0; lb < 2; lb++) {
          ans += dp[0][sa][la][sb][lb];
        }
      }
    }
  }
  cout << ans << endl;
}
int main() {
  scanf("%d", &tn);
  while (tn--) {
    solve();
  }
  return 0;
}
