#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    long long l, r;
    cin >> l >> r;
    long long dp[32][3][3] = {0};
    int i = 31;
    while (i >= 0 && (l & (1 << i)) == (r & (1 << i))) {
      if ((l & (1 << i)) && (r & (1 << i))) {
        cout << 0 << endl;
        break;
      }
      i--;
    }
    if ((l & (1 << i)) && (r & (1 << i))) continue;
    if (i >= 0) {
      dp[i][0][0] = dp[i][0][2] = dp[i][2][0] = 1;
      i--;
      while (i >= 0) {
        memcpy(dp[i], dp[i + 1], sizeof(dp[i]));
        dp[i][1][1] *= 3;
        if (!(l & (1 << i))) {
          if (r & (1 << i)) {
            dp[i][0][1] *= 2;
            dp[i][1][0] *= 2;
            dp[i][0][1] += dp[i + 1][0][2] + dp[i + 1][0][0];
            dp[i][1][0] += dp[i + 1][2][0] + dp[i + 1][0][0];
            dp[i][1][1] += dp[i + 1][0][2] + dp[i + 1][2][0] +
                           2 * dp[i + 1][1][2] + 2 * dp[i + 1][2][1] +
                           dp[i + 1][0][1] + dp[i + 1][1][0];
            dp[i][2][2] = 0;
          } else {
            dp[i][0][1] *= 2;
            dp[i][1][0] *= 2;
            dp[i][1][2] *= 2;
            dp[i][2][1] *= 2;
            dp[i][0][1] += dp[i + 1][0][0];
            dp[i][1][0] += dp[i + 1][0][0];
            dp[i][1][2] += dp[i + 1][0][2];
            dp[i][2][1] += dp[i + 1][2][0];
            dp[i][1][1] += dp[i + 1][0][1] + dp[i + 1][1][0];
          }
        } else {
          if (r & (1 << i)) {
            dp[i][0][1] += dp[i + 1][0][2];
            dp[i][1][0] += dp[i + 1][2][0];
            dp[i][1][1] += 2 * dp[i + 1][1][2] + 2 * dp[i + 1][2][1];
            dp[i][0][0] = dp[i][0][2] = dp[i][2][0] = dp[i][2][2] = 0;
          } else {
            dp[i][1][2] *= 2;
            dp[i][2][1] *= 2;
            dp[i][0][0] = 0;
          }
        }
        i--;
      }
      long long ans = 0;
      for (int j = 0; j < 3; j++) {
        for (int k = 0; k < 3; k++) ans += dp[0][j][k];
      }
      cout << ans << endl;
    } else {
      cout << (l == 0) << endl;
    }
  }
}
