#include <bits/stdc++.h>
using namespace std;
char mod1[32];
char mod2[32];
long long solve(int a, int b) {
  string astring = bitset<32>(a).to_string();
  string bstring = bitset<32>(b).to_string();
  long long answer = 0;
  for (int i = 0; i < 32; i++) {
    for (int j = 0; j < 32; j++) {
      if (astring[i] != '1' || bstring[j] != '1') continue;
      for (int k = 0; k < 32; k++) {
        if (k == i) {
          mod1[k] = '0';
        } else if (k > i) {
          mod1[k] = '-';
        } else {
          mod1[k] = astring[k];
        }
      }
      for (int k = 0; k < 32; k++) {
        if (k == j) {
          mod2[k] = '0';
        } else if (k > j) {
          mod2[k] = '-';
        } else {
          mod2[k] = bstring[k];
        }
      }
      long long thisAnswer = 1;
      for (int k = 0; k < 32; k++) {
        if (mod1[k] == '1' && mod2[k] == '1') {
          thisAnswer = 0;
        }
        if ((mod1[k] == '-' && mod2[k] == '0') ||
            (mod1[k] == '0' && mod2[k] == '-')) {
          thisAnswer *= 2;
        }
        if (mod1[k] == '-' && mod2[k] == '-') {
          thisAnswer *= 3;
        }
      }
      answer += thisAnswer;
    }
  }
  return answer;
}
int main() {
  if (fopen("FILENAME.in", "r")) {
    freopen("FILENAME.in", "r", stdin);
    freopen("FILENAME.out", "w", stdout);
  }
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    int a, b;
    cin >> a >> b;
    b++;
    cout << solve(b, b) - 2 * solve(a, b) + solve(a, a) << endl;
  }
}
