#include <bits/stdc++.h>
using namespace std;
long long dp[30 + 1][2][2][2];
int l, r;
long long solve(int b, bool lesser, bool greater, bool differ) {
  int bl, br;
  if (b == -1) {
    return differ ? 2 : 1;
  }
  if (dp[b][lesser][greater][differ] != -1) {
    return dp[b][lesser][greater][differ];
  }
  bl = (l >> b) & 1;
  br = (r >> b) & 1;
  dp[b][lesser][greater][differ] = 0;
  if (lesser and greater and differ) {
    dp[b][lesser][greater][differ] += solve(b - 1, true, true, true);
    dp[b][lesser][greater][differ] += solve(b - 1, true, true, true);
    dp[b][lesser][greater][differ] += solve(b - 1, true, true, true);
  } else if (lesser and greater and !differ) {
    dp[b][lesser][greater][differ] += solve(b - 1, true, true, false);
    dp[b][lesser][greater][differ] += solve(b - 1, true, true, true);
  } else if (lesser and !greater and differ) {
    if (bl == 1) {
      dp[b][lesser][greater][differ] += solve(b - 1, true, false, true);
    } else {
      dp[b][lesser][greater][differ] += solve(b - 1, true, false, true);
      dp[b][lesser][greater][differ] += solve(b - 1, true, false, true);
      dp[b][lesser][greater][differ] += solve(b - 1, true, true, true);
    }
  } else if (lesser and !greater and !differ) {
    if (bl == 1) {
    } else {
      dp[b][lesser][greater][differ] += solve(b - 1, true, false, false);
      dp[b][lesser][greater][differ] += solve(b - 1, true, false, true);
    }
  } else if (!lesser and greater and differ) {
    if (br == 1) {
      dp[b][lesser][greater][differ] += solve(b - 1, true, true, true);
      dp[b][lesser][greater][differ] += solve(b - 1, false, true, true);
      dp[b][lesser][greater][differ] += solve(b - 1, true, true, true);
    } else {
      dp[b][lesser][greater][differ] += solve(b - 1, false, true, true);
      dp[b][lesser][greater][differ] += solve(b - 1, false, true, true);
    }
  } else if (!lesser and greater and !differ) {
    if (br == 1) {
      dp[b][lesser][greater][differ] += solve(b - 1, true, true, false);
      dp[b][lesser][greater][differ] += solve(b - 1, false, true, true);
    } else {
      dp[b][lesser][greater][differ] += solve(b - 1, false, true, false);
    }
  } else if (!lesser and !greater and differ) {
    if (bl == 1 and br == 1) {
      dp[b][lesser][greater][differ] += solve(b - 1, true, false, true);
    } else if (bl == 1) {
      dp[b][lesser][greater][differ] += solve(b - 1, false, false, true);
    } else if (br == 1) {
      dp[b][lesser][greater][differ] += solve(b - 1, true, false, true);
      dp[b][lesser][greater][differ] += solve(b - 1, false, false, true);
      dp[b][lesser][greater][differ] += solve(b - 1, true, true, true);
    } else {
      dp[b][lesser][greater][differ] += solve(b - 1, false, false, true);
      dp[b][lesser][greater][differ] += solve(b - 1, false, true, true);
    }
  } else if (!lesser and !greater and !differ) {
    if (bl == 1 and br == 1) {
    } else if (bl == 1) {
    } else if (br == 1) {
      dp[b][lesser][greater][differ] += solve(b - 1, true, false, false);
      dp[b][lesser][greater][differ] += solve(b - 1, false, false, true);
    } else {
      dp[b][lesser][greater][differ] += solve(b - 1, false, false, false);
    }
  }
  return dp[b][lesser][greater][differ];
}
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    memset(dp, -1, sizeof(dp));
    scanf("%d%d", &l, &r);
    printf("%lld\n", solve(30, false, false, false));
  }
  return 0;
}
