#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define all(x) x.begin(),x.end()
#define len(x) (int)(x).size()
#define int long long

/*
#pragma GCC omptimize("unroll-loops")
#pragma optimize("SEX_ON_THE_BEACH")
#pragma GCC optimize("no-stack-protector")
#pragma comment(linker, "/STACK:10000000000")
*/

using namespace std;
using namespace __gnu_pbds;

typedef long double ld;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pari;
typedef vector<vi > matrix;

ld PI = 3.14159265358979323846;

typedef tree<int, null_type, less<int >,
    rb_tree_tag, tree_order_statistics_node_update> ordered_set;

void volodya22(){
    #ifdef THERE
        freopen("input.txt", "r", stdin);
        //freopen("output.txt", "w", stdout);
    #else
        //freopen("fibonacci.in", "r", stdin);
        //freopen("fibonacci.out", "w", stdout);
        //#define endl "\n"
        ios_base::sync_with_stdio(0);
        cin.tie(0);
        cout.tie(0);
    #endif

    //cout.precision(6);
    //cout << fixed;

}

struct Zdarovo{
    int l,r,ind;
};

const int N = 1e6+7;
const ll MOD = 1e9+7;

const int INF = 1e18+7;

const int K = 40;

vi L,R;

int dp[K][2][2][2][2];

int DP(int ind,int f1,int f2,int f3,int f4){
    if(ind == K) return 1;

    if(dp[ind][f1][f2][f3][f4] == -1){
        int sum = 0;

        for(int i = 0;i < 2;i++){
            for(int j = 0;j < 2;j++){
                if(i && j) continue;

                if(i == 1 && f2 == 1 && R[ind] == 0)
                    continue;

                if(i == 0 && f1 == 1 && L[ind] == 1)
                    continue;

                if(j == 1 && f4 == 1 && R[ind] == 0)
                    continue;

                if(j == 0 && f3 == 1 && L[ind] == 1)
                    continue;

                int ff1 = f1,ff2 = f2,ff3 = f3,ff4 = f4;

                if(i == 1 && f1 == 1 && L[ind] == 0)
                    ff1 = 0;

                if(i == 0 && f2 == 1 && R[ind] == 1)
                    ff2 = 0;

                if(j == 1 && f3 == 1 && L[ind] == 0)
                    ff3 = 0;

                if(j == 0 && f4 == 1 && R[ind] == 1)
                    ff4 = 0;

                    /*cout << ind << endl;
                    cout << i << ' ' << j << endl;
                    cout << f1 << ' ' << f2 << ' ' << f3 << ' ' << f4  << endl;
                    cout << ff1 << ' ' << ff2 << ' ' << ff3 << ' ' << ff4 << endl;
*/
                sum += DP(ind+1,ff1,ff2,ff3,ff4);

            }
        }

        //cout << ind << ' ' << f1 << ' ' << f2 << ' ' << f3 << ' ' << f4 << ' ' << sum << endl;

        dp[ind][f1][f2][f3][f4] = sum;
    }

    return dp[ind][f1][f2][f3][f4];
}

void input(){
    int t;
    cin >> t;
    while(t--){
        int l,r;
        cin >> l >> r;
        L.clear();
        R.clear();
        int a = 0;
        if(l == 0 && r == 0){
            //cout << 1 << endl;
            continue;
        }
        if(l == 0) l++,a++;
        for(int i = K-1;i >= 0;i--){
            L.push_back(min<int>(1,l&(1ll<<i)));
            R.push_back(min<int>(1,r&(1ll<<i)));
            //cout << R[K-1-i] << ' ';
        }//cout << endl;

        memset(dp,-1,sizeof(dp));

        cout << DP(0,1,1,1,1)+a << endl;;
    }
}

void solve(){

}

signed main(){
    volodya22();
    input();
    solve();
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define all(x) x.begin(),x.end()
#define len(x) (int)(x).size()
#define int long long

/*
#pragma GCC omptimize("unroll-loops")
#pragma optimize("SEX_ON_THE_BEACH")
#pragma GCC optimize("no-stack-protector")
#pragma comment(linker, "/STACK:10000000000")
*/

using namespace std;
using namespace __gnu_pbds;

typedef long double ld;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pari;
typedef vector<vi > matrix;

ld PI = 3.14159265358979323846;

typedef tree<int, null_type, less<int >,
    rb_tree_tag, tree_order_statistics_node_update> ordered_set;

void volodya22(){
    #ifdef THERE
        freopen("input.txt", "r", stdin);
        //freopen("output.txt", "w", stdout);
    #else
        //freopen("fibonacci.in", "r", stdin);
        //freopen("fibonacci.out", "w", stdout);
        //#define endl "\n"
        ios_base::sync_with_stdio(0);
        cin.tie(0);
        cout.tie(0);
    #endif

    //cout.precision(6);
    //cout << fixed;

}

struct Zdarovo{
    int l,r,ind;
};

const int N = 1e6+7;
const ll MOD = 1e9+7;

const int INF = 1e18+7;

const int K = 40;

vi L,R;

int dp[K][2][2][2][2];

int DP(int ind,int f1,int f2,int f3,int f4){
    if(ind == K) return 1;

    if(dp[ind][f1][f2][f3][f4] == -1){
        int sum = 0;

        for(int i = 0;i < 2;i++){
            for(int j = 0;j < 2;j++){
                if(i && j) continue;

                if(i == 1 && f2 == 1 && R[ind] == 0)
                    continue;

                if(i == 0 && f1 == 1 && L[ind] == 1)
                    continue;

                if(j == 1 && f4 == 1 && R[ind] == 0)
                    continue;

                if(j == 0 && f3 == 1 && L[ind] == 1)
                    continue;

                int ff1 = f1,ff2 = f2,ff3 = f3,ff4 = f4;

                if(i == 1 && f1 == 1 && L[ind] == 0)
                    ff1 = 0;

                if(i == 0 && f2 == 1 && R[ind] == 1)
                    ff2 = 0;

                if(j == 1 && f3 == 1 && L[ind] == 0)
                    ff3 = 0;

                if(j == 0 && f4 == 1 && R[ind] == 1)
                    ff4 = 0;

                    /*cout << ind << endl;
                    cout << i << ' ' << j << endl;
                    cout << f1 << ' ' << f2 << ' ' << f3 << ' ' << f4  << endl;
                    cout << ff1 << ' ' << ff2 << ' ' << ff3 << ' ' << ff4 << endl;
*/
                sum += DP(ind+1,ff1,ff2,ff3,ff4);

            }
        }

        //cout << ind << ' ' << f1 << ' ' << f2 << ' ' << f3 << ' ' << f4 << ' ' << sum << endl;

        dp[ind][f1][f2][f3][f4] = sum;
    }

    return dp[ind][f1][f2][f3][f4];
}

void input(){
    int t;
    cin >> t;
    while(t--){
        int l,r;
        cin >> l >> r;
        L.clear();
        R.clear();
        int a = 0;
        if(l == 0 && r == 0){
            cout << 1 << endl;
            continue;
        }
        if(l == 0) l++,a = r+1;
        for(int i = K-1;i >= 0;i--){
            L.push_back(min<int>(1,l&(1ll<<i)));
            R.push_back(min<int>(1,r&(1ll<<i)));
            //cout << R[K-1-i] << ' ';
        }//cout << endl;

        memset(dp,-1,sizeof(dp));

        cout << DP(0,1,1,1,1)+a*2 << endl;;
    }
}

void solve(){

}

signed main(){
    volodya22();
    input();
    solve();
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define all(x) x.begin(),x.end()
#define len(x) (int)(x).size()
#define int long long

/*
#pragma GCC omptimize("unroll-loops")
#pragma optimize("SEX_ON_THE_BEACH")
#pragma GCC optimize("no-stack-protector")
#pragma comment(linker, "/STACK:10000000000")
*/

using namespace std;
using namespace __gnu_pbds;

typedef long double ld;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pari;
typedef vector<vi > matrix;

ld PI = 3.14159265358979323846;

typedef tree<int, null_type, less<int >,
    rb_tree_tag, tree_order_statistics_node_update> ordered_set;

void volodya22(){
    #ifdef THERE
        freopen("input.txt", "r", stdin);
        //freopen("output.txt", "w", stdout);
    #else
        //freopen("fibonacci.in", "r", stdin);
        //freopen("fibonacci.out", "w", stdout);
        //#define endl "\n"
        ios_base::sync_with_stdio(0);
        cin.tie(0);
        cout.tie(0);
    #endif

    //cout.precision(6);
    //cout << fixed;

}

struct Zdarovo{
    int l,r,ind;
};

const int N = 1e6+7;
const ll MOD = 1e9+7;

const int INF = 1e18+7;

const int K = 40;

vi L,R;

int dp[K][2][2][2][2];

int DP(int ind,int f1,int f2,int f3,int f4){
    if(ind == K) return 1;

    if(dp[ind][f1][f2][f3][f4] == -1){
        int sum = 0;

        for(int i = 0;i < 2;i++){
            for(int j = 0;j < 2;j++){
                if(i && j) continue;

                if(i == 1 && f2 == 1 && R[ind] == 0)
                    continue;

                if(i == 0 && f1 == 1 && L[ind] == 1)
                    continue;

                if(j == 1 && f4 == 1 && R[ind] == 0)
                    continue;

                if(j == 0 && f3 == 1 && L[ind] == 1)
                    continue;

                int ff1 = f1,ff2 = f2,ff3 = f3,ff4 = f4;

                if(i == 1 && f1 == 1 && L[ind] == 0)
                    ff1 = 0;

                if(i == 0 && f2 == 1 && R[ind] == 1)
                    ff2 = 0;

                if(j == 1 && f3 == 1 && L[ind] == 0)
                    ff3 = 0;

                if(j == 0 && f4 == 1 && R[ind] == 1)
                    ff4 = 0;

                    /*cout << ind << endl;
                    cout << i << ' ' << j << endl;
                    cout << f1 << ' ' << f2 << ' ' << f3 << ' ' << f4  << endl;
                    cout << ff1 << ' ' << ff2 << ' ' << ff3 << ' ' << ff4 << endl;
*/
                sum += DP(ind+1,ff1,ff2,ff3,ff4);

            }
        }

        //cout << ind << ' ' << f1 << ' ' << f2 << ' ' << f3 << ' ' << f4 << ' ' << sum << endl;

        dp[ind][f1][f2][f3][f4] = sum;
    }

    return dp[ind][f1][f2][f3][f4];
}

void input(){
    int t;
    cin >> t;
    while(t--){
        int l,r;
        cin >> l >> r;
        L.clear();
        R.clear();
        int a = 0;
        if(l == 0 && r == 0){
            cout << 1 << endl;
            continue;
        }
        if(l == 0) l++,a = r+1;
        for(int i = K-1;i >= 0;i--){
            L.push_back(min<int>(1,l&(1ll<<i)));
            R.push_back(min<int>(1,r&(1ll<<i)));
            //cout << R[K-1-i] << ' ';
        }//cout << endl;

        memset(dp,-1,sizeof(dp));

        cout << DP(0,1,1,1,1)+a << endl;;
    }
}

void solve(){

}

signed main(){
    volodya22();
    input();
    solve();
}


//Chaos///

#pragma GCC optimize("O3", "unroll-loops")
#pragma GCC target("avx2")
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#if DEBUG && !ONLINE_JUDGE
    #define debug(args...)     (Debugger()) , args
    class Debugger
    {
        public:
        Debugger(const std::string& _separator = ", ") :
        first(true), separator(_separator){}
        template<typename ObjectType>
        Debugger& operator , (const ObjectType& v)
        {
            if(!first)
                std::cerr << separator;
            std::cerr << v;
            first = false;
            return *this;
        }
        ~Debugger() {  std::cerr << endl;}

        private:
        bool first;
        std::string separator;
    };
    template <typename T1, typename T2>
    inline std::ostream& operator << (std::ostream& os, const std::pair<T1, T2>& p)
    {
        return os << "(" << p.first << ", " << p.second << ")";
    }
    template<typename T>
    inline std::ostream &operator << (std::ostream & os,const std::vector<T>& v)
    {
        bool first = true;
        os << "[";
        for(unsigned int i = 0; i < v.size(); i++)
        {
            if(!first)
                os << ", ";
            os << v[i];
            first = false;
        }
        return os << "]";
    }
    template<typename T>
    inline std::ostream &operator << (std::ostream & os,const std::set<T>& v)
    {
        bool first = true;
        os << "[";
        for (typename std::set<T>::const_iterator ii = v.begin(); ii != v.end(); ++ii)
        {
            if(!first)
                os << ", ";
            os << *ii;
            first = false;
        }
        return os << "]";
    }
    template<typename T1, typename T2>
    inline std::ostream &operator << (std::ostream & os,const std::map<T1, T2>& v)
    {
        bool first = true;
        os << "[";
        for (typename std::map<T1, T2>::const_iterator ii = v.begin(); ii != v.end(); ++ii)
        {
            if(!first)
                os << ", ";
            os << *ii ;
            first = false;
        }
        return os << "]";
    }
#else
    #define debug(args...)
#endif
const int mod = 1000000007;
// const int mod = 998244353;
#define ok order_of_key
#define fo find_by_order
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define ll long long
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define repp(i,n) for(int i=(0);i<(n);i++)
#define repr(i,n) for(int i=(n);i>=0;i--)
#define reprr(i,n) for(int i=(n);i>0;i--)
#define repl(i,a,b) for(ll i=(a);i<(b);i++)
#define newl cout<<'\n'
#define inf 1000000000
#define repaa(i,a) for(auto i=a.rbegin();i!=a.rend();++i)
#define pb push_back
#define test cout<<"test"<<endl
#define tc int tt;cin>>tt;while(tt--)
#define startk int tt;cin>>tt;for(int kk=1;kk<=tt;kk++)
#define mem(v,i) memset(v,i,sizeof(v))//Use mem(v,60) to initialize with 10^9
#define all(x) x.begin(), x.end()
#define inf1 1e18
#define repa(i,a) for (auto i : a)
#define X first
#define Y second
#define lb lower_bound
#define ub upper_bound
#define gcd(a,b)    __gcd((a),(b))
#define lcm(a,b)    ((a)*(b)) / gcd((a),(b))
#define in(a,n) repp(jjj,n)cin>>a[jjj];
#define intree(a,m) repp(i,m){int tmp1,tmp2;cin>>tmp1>>tmp2;tmp1--,tmp2--;a[tmp1].eb(tmp2);a[tmp2].eb(tmp1);}
#define ins insert
#define er erase
#define sz(a) (int)(a.size())
#define print(x,n,k) repp(i,n){repp(j,k)cout<<x[i][j]<<"  ";newl;}
#define mt make_tuple
#define eb emplace_back
#define bpl(i) __builtin_popcountll(i)
#define bp(i) __builtin_popcount(i)
#define precision(x) cout << fixed << setprecision(x);
#define si set<int>
#define usi unordered_set<int>
#define sl set < ll >
#define usl unordered_set < ll >
#define sip set < ip >
#define sil set < il >
#define mi map<int , int >
#define umi unordered_map<int , int >
#define ml map < ll , ll>
#define uml unordered_map < ll , ll>
#define mip map<ip , int>
#define mmx(x1,x2) min(x1,x2) , max(x1,x2)
#define make_unique(x) sort(all((x))); (x).resize(unique(all((x))) - (x).begin())
#define mxe max_element
#define mne min_element
#define srt(v) sort(all(v))
#define rsrt(v) sort(v.rbegin(), v.rend())
typedef long double ld;
typedef vector<int> vi;
typedef vector<char> vc;
typedef vector<ll> vl;
typedef vector < vi > vii;
typedef vector<pair<int,int>> vip;
typedef vector<pair<ll,ll>> vil;
typedef vector < vl > vll;
typedef pair<int, int> ip;
typedef pair<ll, ll> il;
typedef tuple<int,int,int> state;
typedef tuple<ll,ll,ll> statel;
const int dx[4]={1,0,0,-1};
const int dy[4]={0,1,-1,0};
const int dxo[8] = {-1, -1, -1, 0, 1, 1, 1, 0};
const int dyo[8] = {-1, 0, 1, 1, 1, 0, -1, -1};
const double eps = 1e-10;
const char nl = '\n';
const char sp = ' ';
#ifdef Fading
#define gc getchar
#endif
#ifndef Fading
inline char gc(){static char now[1<<16],*S,*T;if (T==S){T=(S=now)+fread(now,1,1<<16,stdin);if (T==S) return EOF;}return *S++;}
#endif
inline ll read(){register ll x=0,f=1;char ch=gc();while (!isdigit(ch)){if(ch=='-')f=-1;ch=gc();}while (isdigit(ch)){x=(x<<3)+(x<<1)+ch-'0';ch=gc();}return (f==1)?x:-x;}
template<typename T>T Min(T v) {return v;}template<typename T, typename... Args>T Min(T first, Args... args) {return min(first , Min(args...));}
template<typename T>T Max(T v) {return v;}
template<typename T, typename... Args> T Max(T first, Args... args) {return max(first , Max(args...));}
template<typename T> T mult(T v) {return v;} template<typename T, typename... Args>T mult(T first, Args... args) {return mult((first%mod * mult(args...))%mod);}
template<class T , class U> inline bool remax(T &a, U b) { return a < b ? a = b, 1 : 0; }
template<class T , class U> inline bool remin(T &a, U b) { return a > b ? a = b, 1 : 0; }
template<class A, class B> void add(A &a, B b){a += b;if(a >= mod) a -= mod;if(a < 0) a += mod;}
template<class A, class B> ll addr(A a, B b){a += b;if(a >= mod) a -= mod;if(a < 0) a += mod;return a;}
template<class T1,class T2>T2 min(T1 a,T2 b){return min((T2)a,b);}
template<class T1,class T2>T2 max(T1 a,T2 b){return max((T2)a,b);}
template<class A> void pnt(A a[],int st,int en) {rep(i,st,en) cout << a[i] << ' ' ; newl;}
template<class A> void pnt(A a[],int en) {repp(i,en) cout << a[i] << ' ' ; newl;}
template<class A> void pnt(vector<A>&a, char tmp = '\n') {repa(i,a) cout << i << ' ' ; newl;}
template<class A> void pnt(A &a , char tmp = '\n') {cout << a << tmp;}
template<class A> void pnt(set<A>&a , char tmp = '\n') {repa(i,a) cout << i << ' ' ; newl;}
template<class A> void pnt(unordered_set<A>&a, char tmp = '\n') {repa(i,a) cout << i << ' ' ; newl;}
template<class A ,class B> void pnt(pair<A,B>&a, char tmp = '\n'){cout << a.X << ' ' << a.Y << tmp;}
template<typename T1,typename T2>inline istream& operator>>(istream&i,pair<T1,T2>&v){return i>>v.X>>v.Y;}
template<typename T , typename A>void W(T v ,A &c) {pnt(v,c);}template<typename T, typename A , typename... Args>void W(T first,A &c , Args... args) {pnt(first,c) ; W(args...);}
template <class T1>void kill(T1 ans){pnt(ans);exit(0);}

ll nc2(ll a){return a * (a - 1) / 2;}
ll setbit(ll num, ll kk){ return 1LL*(num|(1LL<<kk)); }
ll unsetbit(ll num, ll kk){ return 1LL*(num&~(1LL<<kk)); }
ll invertbit(ll num, ll kk){ return 1LL*(num^(1LL<<kk)); }
bool kthbit(ll num, ll kk){ return num&(1LL<<kk); }
ll mceil(ll a, ll b){return a/b + (ll)(a%b > 0);}
ll poww(ll a, ll b,ll m = mod){ll val = 1;a %= m;while (b > 0){if (b & 1) val = (a * val) % m;a = (a * a) % m;b >>= 1;}return val;}
ll inv(ll a){return poww(a , mod - 2);}
ll gcdll(ll x, ll y){return y == 0 ? x : gcdll(y, x % y);}
int mylog(ll x , ll y){if(x == 0) return -1;return mylog(x/y,y) + 1;}

//-----------------------------------------------------end--------------------------------------//

ll dp[34][2][2][2][2];
ll l , r;

ll solve(int le , bool fl1 , bool fl2 , bool fl3 , bool fl4){
    if(le > 33) return 1;
    ll &res = dp[le][fl1][fl2][fl3][fl4];
    if(res != -1) return res;
    int st1 = fl1 ? 0 : kthbit(l,33 - le);
    int en1 = fl2 ? 1 : kthbit(r,33 - le);
    int st2 = fl3 ? 0 : kthbit(l,33 - le);
    int en2 = fl4 ? 1 : kthbit(r,33 - le);
    res = 0;
    rep(i,st1,en1 + 1){
        rep(j,st2,en2 + 1){
            if(i == 1 && j == 1) continue;
            bool f1 = fl1 | (i > kthbit(l,33 - le));
            bool f2 = fl2 | (i < kthbit(r,33 - le));
            bool f3 = fl3 | (j > kthbit(l,33 - le));
            bool f4 = fl4 | (j < kthbit(r,33 - le));
            res += solve(le + 1 , f1 , f2 , f3 , f4);
        }
    }
    return res;
}

signed main()
{
    ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    tc{
        cin >> l >> r;
        mem(dp , -1);
        W(solve(0,0,0,0,0) , nl);
    }
}


//Chaos///

#pragma GCC optimize("O3", "unroll-loops")
#pragma GCC target("avx2")
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#if DEBUG && !ONLINE_JUDGE
    #define debug(args...)     (Debugger()) , args
    class Debugger
    {
        public:
        Debugger(const std::string& _separator = ", ") :
        first(true), separator(_separator){}
        template<typename ObjectType>
        Debugger& operator , (const ObjectType& v)
        {
            if(!first)
                std::cerr << separator;
            std::cerr << v;
            first = false;
            return *this;
        }
        ~Debugger() {  std::cerr << endl;}

        private:
        bool first;
        std::string separator;
    };
    template <typename T1, typename T2>
    inline std::ostream& operator << (std::ostream& os, const std::pair<T1, T2>& p)
    {
        return os << "(" << p.first << ", " << p.second << ")";
    }
    template<typename T>
    inline std::ostream &operator << (std::ostream & os,const std::vector<T>& v)
    {
        bool first = true;
        os << "[";
        for(unsigned int i = 0; i < v.size(); i++)
        {
            if(!first)
                os << ", ";
            os << v[i];
            first = false;
        }
        return os << "]";
    }
    template<typename T>
    inline std::ostream &operator << (std::ostream & os,const std::set<T>& v)
    {
        bool first = true;
        os << "[";
        for (typename std::set<T>::const_iterator ii = v.begin(); ii != v.end(); ++ii)
        {
            if(!first)
                os << ", ";
            os << *ii;
            first = false;
        }
        return os << "]";
    }
    template<typename T1, typename T2>
    inline std::ostream &operator << (std::ostream & os,const std::map<T1, T2>& v)
    {
        bool first = true;
        os << "[";
        for (typename std::map<T1, T2>::const_iterator ii = v.begin(); ii != v.end(); ++ii)
        {
            if(!first)
                os << ", ";
            os << *ii ;
            first = false;
        }
        return os << "]";
    }
#else
    #define debug(args...)
#endif
const int mod = 1000000007;
// const int mod = 998244353;
#define ok order_of_key
#define fo find_by_order
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define ll long long
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define repp(i,n) for(int i=(0);i<(n);i++)
#define repr(i,n) for(int i=(n);i>=0;i--)
#define reprr(i,n) for(int i=(n);i>0;i--)
#define repl(i,a,b) for(ll i=(a);i<(b);i++)
#define newl cout<<'\n'
#define inf 1000000000
#define repaa(i,a) for(auto i=a.rbegin();i!=a.rend();++i)
#define pb push_back
#define test cout<<"test"<<endl
#define tc int tt;cin>>tt;while(tt--)
#define startk int tt;cin>>tt;for(int kk=1;kk<=tt;kk++)
#define mem(v,i) memset(v,i,sizeof(v))//Use mem(v,60) to initialize with 10^9
#define all(x) x.begin(), x.end()
#define inf1 1e18
#define repa(i,a) for (auto i : a)
#define X first
#define Y second
#define lb lower_bound
#define ub upper_bound
#define gcd(a,b)    __gcd((a),(b))
#define lcm(a,b)    ((a)*(b)) / gcd((a),(b))
#define in(a,n) repp(jjj,n)cin>>a[jjj];
#define intree(a,m) repp(i,m){int tmp1,tmp2;cin>>tmp1>>tmp2;tmp1--,tmp2--;a[tmp1].eb(tmp2);a[tmp2].eb(tmp1);}
#define ins insert
#define er erase
#define sz(a) (int)(a.size())
#define print(x,n,k) repp(i,n){repp(j,k)cout<<x[i][j]<<"  ";newl;}
#define mt make_tuple
#define eb emplace_back
#define bpl(i) __builtin_popcountll(i)
#define bp(i) __builtin_popcount(i)
#define precision(x) cout << fixed << setprecision(x);
#define si set<int>
#define usi unordered_set<int>
#define sl set < ll >
#define usl unordered_set < ll >
#define sip set < ip >
#define sil set < il >
#define mi map<int , int >
#define umi unordered_map<int , int >
#define ml map < ll , ll>
#define uml unordered_map < ll , ll>
#define mip map<ip , int>
#define mmx(x1,x2) min(x1,x2) , max(x1,x2)
#define make_unique(x) sort(all((x))); (x).resize(unique(all((x))) - (x).begin())
#define mxe max_element
#define mne min_element
#define srt(v) sort(all(v))
#define rsrt(v) sort(v.rbegin(), v.rend())
#define no W("No" , nl)
#define yes W("Yes" , nl)
#define yesc W("YES" , nl)
#define noc W("NO" , nl)
typedef long double ld;
typedef vector<int> vi;
typedef vector<char> vc;
typedef vector<ll> vl;
typedef vector < vi > vii;
typedef vector<pair<int,int>> vip;
typedef vector<pair<ll,ll>> vil;
typedef vector < vl > vll;
typedef pair<int, int> ip;
typedef pair<ll, ll> il;
typedef tuple<int,int,int> state;
typedef tuple<ll,ll,ll> statel;
const int dx[4]={1,0,0,-1};
const int dy[4]={0,1,-1,0};
const int dxo[8] = {-1, -1, -1, 0, 1, 1, 1, 0};
const int dyo[8] = {-1, 0, 1, 1, 1, 0, -1, -1};
const double eps = 1e-10;
const char nl = '\n';
const char sp = ' ';
#ifdef Fading
#define gc getchar
#endif
#ifndef Fading
inline char gc(){static char now[1<<16],*S,*T;if (T==S){T=(S=now)+fread(now,1,1<<16,stdin);if (T==S) return EOF;}return *S++;}
#endif
inline ll read(){register ll x=0,f=1;char ch=gc();while (!isdigit(ch)){if(ch=='-')f=-1;ch=gc();}while (isdigit(ch)){x=(x<<3)+(x<<1)+ch-'0';ch=gc();}return (f==1)?x:-x;}
template<typename T>T Min(T v) {return v;}template<typename T, typename... Args>T Min(T first, Args... args) {return min(first , Min(args...));}
template<typename T>T Max(T v) {return v;}
template<typename T, typename... Args> T Max(T first, Args... args) {return max(first , Max(args...));}
template<typename T> T mult(T v) {return v;} template<typename T, typename... Args>T mult(T first, Args... args) {return mult((first%mod * mult(args...))%mod);}
template<class T , class U> inline bool remax(T &a, U b) { return a < b ? a = b, 1 : 0; }
template<class T , class U> inline bool remin(T &a, U b) { return a > b ? a = b, 1 : 0; }
template<class A, class B> void add(A &a, B b){a += b;if(a >= mod) a -= mod;if(a < 0) a += mod;}
template<class A, class B> ll addr(A a, B b){a += b;if(a >= mod) a -= mod;if(a < 0) a += mod;return a;}
template<class T1,class T2>T2 min(T1 a,T2 b){return min((T2)a,b);}
template<class T1,class T2>T2 max(T1 a,T2 b){return max((T2)a,b);}
template<class A> void pnt(A a[],int st,int en) {rep(i,st,en) cout << a[i] << ' ' ; newl;}
template<class A> void pnt(A a[],int en) {repp(i,en) cout << a[i] << ' ' ; newl;}
template<class A> void pnt(vector<A>&a, char tmp = '\n') {repa(i,a) cout << i << ' ' ; newl;}
template<class A> void pnt(A &a , char tmp = '\n') {cout << a << tmp;}
template<class A> void pnt(set<A>&a , char tmp = '\n') {repa(i,a) cout << i << ' ' ; newl;}
template<class A> void pnt(unordered_set<A>&a, char tmp = '\n') {repa(i,a) cout << i << ' ' ; newl;}
template<class A ,class B> void pnt(pair<A,B>&a, char tmp = '\n'){cout << a.X << ' ' << a.Y << tmp;}
template<typename T1,typename T2>inline istream& operator>>(istream&i,pair<T1,T2>&v){return i>>v.X>>v.Y;}
template<typename T , typename A>void W(T v ,A &c) {pnt(v,c);}template<typename T, typename A , typename... Args>void W(T first,A &c , Args... args) {pnt(first,c) ; W(args...);}
template <class T1>void kill(T1 ans){pnt(ans);exit(0);}

ll nc2(ll a){return a * (a - 1) / 2;}
ll setbit(ll num, ll kk){ return 1LL*(num|(1LL<<kk)); }
ll unsetbit(ll num, ll kk){ return 1LL*(num&~(1LL<<kk)); }
ll invertbit(ll num, ll kk){ return 1LL*(num^(1LL<<kk)); }
bool kthbit(ll num, ll kk){ return num&(1LL<<kk); }
ll mceil(ll a, ll b){return a/b + (ll)(a%b > 0);}
ll poww(ll a, ll b,ll m = mod){ll val = 1;a %= m;while (b > 0){if (b & 1) val = (a * val) % m;a = (a * a) % m;b >>= 1;}return val;}
ll inv(ll a){return poww(a , mod - 2);}
ll gcdll(ll x, ll y){return y == 0 ? x : gcdll(y, x % y);}
int mylog(ll x , ll y){if(x == 0) return -1;return mylog(x/y,y) + 1;}

//-----------------------------------------------------end--------------------------------------//

#define int ll

ll ncr[34][34] , pw[34];

inline int get(int a){
    return 1 << a;
}

inline int solve1(int l , int ps){
    int _ = mylog(l,2);
    vi pos;
    pos.eb(-1);
    repp(i,_ + 1) if(kthbit(l,i)) pos.eb(i);
    ll ans = 0;
    pos.eb(ps);debug(pos);
    rep(i,1,pos.size()){
        int nxt = pos[i] , prev = pos[i - 1];
        int bitsLeft = nxt - prev - 1;
        repp(_0, bitsLeft + 1){
            ll ways = ncr[bitsLeft][_0];
            int ch1 = prev + 1;
            int ch2 = pos.size() - i - 1;
            int ch3 = ps - nxt - ch2;
            ans += ways * (get(_0) - 1) * pw[ch1] * get(ch3);
        }
    }
    pos.pop_back();
    ans += get(ps - pos.back() - 1);
    debug(ps,ans);
    return ans;
}

inline int solve(int l , int r){
    int mxbit = mylog(r,2);
    vi pos;
    pos.eb(-1);
    ll ans = 0;
    vi cnt(mxbit);
    int _ = mylog(l,2);
    repp(i,_ + 1) if(kthbit(l,i)) pos.eb(i);
    int c_zero = 0;
    repr(i,mxbit - 1){
        if(i == pos.back()){
            if(r != -1) {
                int val = 0 , cc = 0;
                repr(j,i){
                    if(kthbit(r,j) == 0) cc ++;
                    else {
                        val += pw[j] * get(cc + 1);
                    }
                }
                val += get(cc);
                ans += (get(c_zero) - 1) * val;
                if(kthbit(r,i) || i == 0){
                    cnt[i] ++;
                    r = -1;
                }
            }
            c_zero = 0;
            pos.pop_back();
            int newPos = pos.back();
            int bitsLeft = i - newPos - 1;
            int tmp = 0;
            if(newPos == -1) tmp = 1;
            repp(_0, bitsLeft + 1){
                int ways = ncr[bitsLeft][_0];
                ans += cnt[i] * ways * pw[pos.back() + 1] * (get(_0) - 1 + tmp);
            }
            if(pos.back() != -1) cnt[pos.back()] += cnt[i] * get(bitsLeft);
            continue;
        }
        if(r == -1) continue;
        if(kthbit(r,i) == 0) {
            c_zero ++;
            continue;
        }
        int bitsLeft = i - pos.back() - 1;
        repp(_0, bitsLeft + 1){
            int tot0 = c_zero + _0 + 1;
            int ways = ncr[bitsLeft][_0];
            ans += ways * pw[pos.back() + 1] * (get(tot0) - 1);
        }
        if(pos.back() != -1) cnt[pos.back()] += get(bitsLeft);
    }debug(ans,"d");
    return ans;
}

// inline int solve(int l, int r){
//     int mxbit = mylog(r,2);
//     vi cnt(mxbit);
//     vi pos;
//     pos.eb(-1);
//     int _ = mylog(l,2);
//     ll ans = 0;
//     repr(i,_) if(kthbit(l,i)) pos.eb(i);
//     int c_zero = 0;
//     repr(i,mxbit - 1){
//         if(i == pos.back()) {
//             ans += (get(c_zero) - 1) * get(pos.back() + 1);
//             c_zero = 0;
//             int oldPos = pos.back();
//             if(mxbit == ps && kthbit(r,i) == 1){
//                 cnt[oldPos] ++;
//                 r = 0;
//             }
//             else if(mxbit != ps) cnt[oldPos] ++;
//             int help = cnt[oldPos];
//             pos.pop_back();
//             int newPos = pos.back();
//             int bitsLeft = oldPos - newPos - 1;
//             int tmp = pos.size() == 1;
//             repp(_0, bitsLeft + 1){
//                 int tot0 = _0;
//                 ll ways = ncr[bitsLeft][_0];
//                 ans += help * ways * (get(tot0) - 1 + tmp) * pw[pos.back() + 1];
//             }
//             debug(ans,i,ps,bitsLeft,"bruh");
//             if(pos.back() != -1) cnt[pos.back()] += help * get(bitsLeft);
//             continue;
//         }
//         if(kthbit(r,i) == 0) {
//             c_zero ++;
//             continue;
//         }
//         int bitsLeft = i - pos.back() - 1;debug(bitsLeft,i ,"lol");
//         repp(_0, bitsLeft + 1){
//             int tot0 = c_zero + _0 + 1;
//             ll ways = ncr[bitsLeft][_0];
//             ans += ways * (get(tot0) - 1) * pw[pos.back() + 1];
//         }
//         if(pos.back() != -1) cnt[pos.back()] += get(bitsLeft);
//     }
//     ans += get(c_zero) - 1;
//     debug(ps,ans);
//     return ans;
// }
// 100 // 10 // 11

void pre(){
    repp(i,34) repp(j,34) {
        if(j > i) ncr[i][j] = 0;
        else if(j == i || j == 0) ncr[i][j] = 1;
    }
    rep(i,2,34){
        rep(j,1,i) ncr[i][j] = ncr[i - 1][j] + ncr[i - 1][j - 1];
    }
    pw[0] = 1;
    rep(i,1,34) pw[i] = pw[i - 1] * 3;
}

signed main()
{
    ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    pre();
    tc{
        int l , r;
        cin >> l >> r;
        if(l == r) {
            W(0,nl);
            continue;
        }
        while(1){
            int x1 = mylog(l,2) , x2 = mylog(r,2);
            if(x1 == x2) l -= (1 << x1) , r -= (1 << x2);
            else break;
        }
        int pos1 = mylog(l,2) , pos2 = mylog(r,2);
        ll ans = 0;
        rep(i,pos1 + 1, pos2) ans += solve1(l,i);
        ans += solve(l,r);
        W(ans << 1 , nl);
    }
}


//Chaos///

#pragma GCC optimize("O3", "unroll-loops")
#pragma GCC target("avx2")
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#if DEBUG && !ONLINE_JUDGE
    #define debug(args...)     (Debugger()) , args
    class Debugger
    {
        public:
        Debugger(const std::string& _separator = ", ") :
        first(true), separator(_separator){}
        template<typename ObjectType>
        Debugger& operator , (const ObjectType& v)
        {
            if(!first)
                std::cerr << separator;
            std::cerr << v;
            first = false;
            return *this;
        }
        ~Debugger() {  std::cerr << endl;}

        private:
        bool first;
        std::string separator;
    };
    template <typename T1, typename T2>
    inline std::ostream& operator << (std::ostream& os, const std::pair<T1, T2>& p)
    {
        return os << "(" << p.first << ", " << p.second << ")";
    }
    template<typename T>
    inline std::ostream &operator << (std::ostream & os,const std::vector<T>& v)
    {
        bool first = true;
        os << "[";
        for(unsigned int i = 0; i < v.size(); i++)
        {
            if(!first)
                os << ", ";
            os << v[i];
            first = false;
        }
        return os << "]";
    }
    template<typename T>
    inline std::ostream &operator << (std::ostream & os,const std::set<T>& v)
    {
        bool first = true;
        os << "[";
        for (typename std::set<T>::const_iterator ii = v.begin(); ii != v.end(); ++ii)
        {
            if(!first)
                os << ", ";
            os << *ii;
            first = false;
        }
        return os << "]";
    }
    template<typename T1, typename T2>
    inline std::ostream &operator << (std::ostream & os,const std::map<T1, T2>& v)
    {
        bool first = true;
        os << "[";
        for (typename std::map<T1, T2>::const_iterator ii = v.begin(); ii != v.end(); ++ii)
        {
            if(!first)
                os << ", ";
            os << *ii ;
            first = false;
        }
        return os << "]";
    }
#else
    #define debug(args...)
#endif
const int mod = 1000000007;
// const int mod = 998244353;
#define ok order_of_key
#define fo find_by_order
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define ll long long
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define repp(i,n) for(int i=(0);i<(n);i++)
#define repr(i,n) for(int i=(n);i>=0;i--)
#define reprr(i,n) for(int i=(n);i>0;i--)
#define repl(i,a,b) for(ll i=(a);i<(b);i++)
#define newl cout<<'\n'
#define inf 1000000000
#define repaa(i,a) for(auto i=a.rbegin();i!=a.rend();++i)
#define pb push_back
#define test cout<<"test"<<endl
#define tc int tt;cin>>tt;while(tt--)
#define startk int tt;cin>>tt;for(int kk=1;kk<=tt;kk++)
#define mem(v,i) memset(v,i,sizeof(v))//Use mem(v,60) to initialize with 10^9
#define all(x) x.begin(), x.end()
#define inf1 1e18
#define repa(i,a) for (auto i : a)
#define X first
#define Y second
#define lb lower_bound
#define ub upper_bound
#define gcd(a,b)    __gcd((a),(b))
#define lcm(a,b)    ((a)*(b)) / gcd((a),(b))
#define in(a,n) repp(jjj,n)cin>>a[jjj];
#define intree(a,m) repp(i,m){int tmp1,tmp2;cin>>tmp1>>tmp2;tmp1--,tmp2--;a[tmp1].eb(tmp2);a[tmp2].eb(tmp1);}
#define ins insert
#define er erase
#define sz(a) (int)(a.size())
#define print(x,n,k) repp(i,n){repp(j,k)cout<<x[i][j]<<"  ";newl;}
#define mt make_tuple
#define eb emplace_back
#define bpl(i) __builtin_popcountll(i)
#define bp(i) __builtin_popcount(i)
#define precision(x) cout << fixed << setprecision(x);
#define si set<int>
#define usi unordered_set<int>
#define sl set < ll >
#define usl unordered_set < ll >
#define sip set < ip >
#define sil set < il >
#define mi map<int , int >
#define umi unordered_map<int , int >
#define ml map < ll , ll>
#define uml unordered_map < ll , ll>
#define mip map<ip , int>
#define mmx(x1,x2) min(x1,x2) , max(x1,x2)
#define make_unique(x) sort(all((x))); (x).resize(unique(all((x))) - (x).begin())
#define mxe max_element
#define mne min_element
#define srt(v) sort(all(v))
#define rsrt(v) sort(v.rbegin(), v.rend())
#define no W("No" , nl)
#define yes W("Yes" , nl)
#define yesc W("YES" , nl)
#define noc W("NO" , nl)
typedef long double ld;
typedef vector<int> vi;
typedef vector<char> vc;
typedef vector<ll> vl;
typedef vector < vi > vii;
typedef vector<pair<int,int>> vip;
typedef vector<pair<ll,ll>> vil;
typedef vector < vl > vll;
typedef pair<int, int> ip;
typedef pair<ll, ll> il;
typedef tuple<int,int,int> state;
typedef tuple<ll,ll,ll> statel;
const int dx[4]={1,0,0,-1};
const int dy[4]={0,1,-1,0};
const int dxo[8] = {-1, -1, -1, 0, 1, 1, 1, 0};
const int dyo[8] = {-1, 0, 1, 1, 1, 0, -1, -1};
const double eps = 1e-10;
const char nl = '\n';
const char sp = ' ';
#ifdef Fading
#define gc getchar
#endif
#ifndef Fading
inline char gc(){static char now[1<<16],*S,*T;if (T==S){T=(S=now)+fread(now,1,1<<16,stdin);if (T==S) return EOF;}return *S++;}
#endif
inline ll read(){register ll x=0,f=1;char ch=gc();while (!isdigit(ch)){if(ch=='-')f=-1;ch=gc();}while (isdigit(ch)){x=(x<<3)+(x<<1)+ch-'0';ch=gc();}return (f==1)?x:-x;}
template<typename T>T Min(T v) {return v;}template<typename T, typename... Args>T Min(T first, Args... args) {return min(first , Min(args...));}
template<typename T>T Max(T v) {return v;}
template<typename T, typename... Args> T Max(T first, Args... args) {return max(first , Max(args...));}
template<typename T> T mult(T v) {return v;} template<typename T, typename... Args>T mult(T first, Args... args) {return mult((first%mod * mult(args...))%mod);}
template<class T , class U> inline bool remax(T &a, U b) { return a < b ? a = b, 1 : 0; }
template<class T , class U> inline bool remin(T &a, U b) { return a > b ? a = b, 1 : 0; }
template<class A, class B> void add(A &a, B b){a += b;if(a >= mod) a -= mod;if(a < 0) a += mod;}
template<class A, class B> ll addr(A a, B b){a += b;if(a >= mod) a -= mod;if(a < 0) a += mod;return a;}
template<class T1,class T2>T2 min(T1 a,T2 b){return min((T2)a,b);}
template<class T1,class T2>T2 max(T1 a,T2 b){return max((T2)a,b);}
template<class A> void pnt(A a[],int st,int en) {rep(i,st,en) cout << a[i] << ' ' ; newl;}
template<class A> void pnt(A a[],int en) {repp(i,en) cout << a[i] << ' ' ; newl;}
template<class A> void pnt(vector<A>&a, char tmp = '\n') {repa(i,a) cout << i << ' ' ; newl;}
template<class A> void pnt(A &a , char tmp = '\n') {cout << a << tmp;}
template<class A> void pnt(set<A>&a , char tmp = '\n') {repa(i,a) cout << i << ' ' ; newl;}
template<class A> void pnt(unordered_set<A>&a, char tmp = '\n') {repa(i,a) cout << i << ' ' ; newl;}
template<class A ,class B> void pnt(pair<A,B>&a, char tmp = '\n'){cout << a.X << ' ' << a.Y << tmp;}
template<typename T1,typename T2>inline istream& operator>>(istream&i,pair<T1,T2>&v){return i>>v.X>>v.Y;}
template<typename T , typename A>void W(T v ,A &c) {pnt(v,c);}template<typename T, typename A , typename... Args>void W(T first,A &c , Args... args) {pnt(first,c) ; W(args...);}
template <class T1>void kill(T1 ans){pnt(ans);exit(0);}

ll nc2(ll a){return a * (a - 1) / 2;}
ll setbit(ll num, ll kk){ return 1LL*(num|(1LL<<kk)); }
ll unsetbit(ll num, ll kk){ return 1LL*(num&~(1LL<<kk)); }
ll invertbit(ll num, ll kk){ return 1LL*(num^(1LL<<kk)); }
bool kthbit(ll num, ll kk){ return num&(1LL<<kk); }
ll mceil(ll a, ll b){return a/b + (ll)(a%b > 0);}
ll poww(ll a, ll b,ll m = mod){ll val = 1;a %= m;while (b > 0){if (b & 1) val = (a * val) % m;a = (a * a) % m;b >>= 1;}return val;}
ll inv(ll a){return poww(a , mod - 2);}
ll gcdll(ll x, ll y){return y == 0 ? x : gcdll(y, x % y);}
int mylog(ll x , ll y){if(x == 0) return -1;return mylog(x/y,y) + 1;}

//-----------------------------------------------------end--------------------------------------//

#define int ll

ll ncr[34][34] , pw[34];

inline int get(int a){
    return 1 << a;
}

inline int solve1(int l , int ps){
    int _ = mylog(l,2);
    vi pos;
    pos.eb(-1);
    repp(i,_ + 1) if(kthbit(l,i)) pos.eb(i);
    ll ans = 0;
    pos.eb(ps);debug(pos);
    rep(i,1,pos.size()){
        int nxt = pos[i] , prev = pos[i - 1];
        int bitsLeft = nxt - prev - 1;
        repp(_0, bitsLeft + 1){
            ll ways = ncr[bitsLeft][_0];
            int ch1 = prev + 1;
            int ch2 = pos.size() - i - 1;
            int ch3 = ps - nxt - ch2;if(ch3 < 0) debug(ch2,ps,nxt,i) , assert(0);
            ans += ways * (get(_0) - 1) * pw[ch1] * get(ch3);
        }
    }
    pos.pop_back();
    ans += get(ps - pos.back() - 1);
    debug(ps,ans);
    return ans;
}

inline int solve(int l , int r){
    int mxbit = mylog(r,2);
    vi pos;
    pos.eb(-1);
    ll ans = 0;
    vi cnt(mxbit);
    int _ = mylog(l,2);
    repp(i,_ + 1) if(kthbit(l,i)) pos.eb(i);
    int c_zero = 0;
    repr(i,mxbit - 1){
        if(i == pos.back()){
            if(r != -1) {
                int val = 0 , cc = 0;
                repr(j,i){
                    if(kthbit(r,j) == 0) cc ++;
                    else {
                        val += pw[j] * get(cc + 1);
                    }
                }
                val += get(cc);
                ans += (get(c_zero) - 1) * val;
                if(kthbit(r,i) || i == 0){
                    cnt[i] ++;
                    r = -1;
                }
            }
            c_zero = 0;
            pos.pop_back();
            int newPos = pos.back();
            int bitsLeft = i - newPos - 1;
            int tmp = 0;
            if(newPos == -1) tmp = 1;
            repp(_0, bitsLeft + 1){
                int ways = ncr[bitsLeft][_0];
                ans += cnt[i] * ways * pw[pos.back() + 1] * (get(_0) - 1 + tmp);
            }
            if(pos.back() != -1) cnt[pos.back()] += cnt[i] * get(bitsLeft);
            continue;
        }
        if(r == -1) continue;
        if(kthbit(r,i) == 0) {
            c_zero ++;
            continue;
        }
        int bitsLeft = i - pos.back() - 1;
        repp(_0, bitsLeft + 1){
            int tot0 = c_zero + _0 + 1;
            int ways = ncr[bitsLeft][_0];
            ans += ways * pw[pos.back() + 1] * (get(tot0) - 1);
        }
        if(pos.back() != -1) cnt[pos.back()] += get(bitsLeft);
    }debug(ans,"d");
    return ans;
}

// inline int solve(int l, int r){
//     int mxbit = mylog(r,2);
//     vi cnt(mxbit);
//     vi pos;
//     pos.eb(-1);
//     int _ = mylog(l,2);
//     ll ans = 0;
//     repr(i,_) if(kthbit(l,i)) pos.eb(i);
//     int c_zero = 0;
//     repr(i,mxbit - 1){
//         if(i == pos.back()) {
//             ans += (get(c_zero) - 1) * get(pos.back() + 1);
//             c_zero = 0;
//             int oldPos = pos.back();
//             if(mxbit == ps && kthbit(r,i) == 1){
//                 cnt[oldPos] ++;
//                 r = 0;
//             }
//             else if(mxbit != ps) cnt[oldPos] ++;
//             int help = cnt[oldPos];
//             pos.pop_back();
//             int newPos = pos.back();
//             int bitsLeft = oldPos - newPos - 1;
//             int tmp = pos.size() == 1;
//             repp(_0, bitsLeft + 1){
//                 int tot0 = _0;
//                 ll ways = ncr[bitsLeft][_0];
//                 ans += help * ways * (get(tot0) - 1 + tmp) * pw[pos.back() + 1];
//             }
//             debug(ans,i,ps,bitsLeft,"bruh");
//             if(pos.back() != -1) cnt[pos.back()] += help * get(bitsLeft);
//             continue;
//         }
//         if(kthbit(r,i) == 0) {
//             c_zero ++;
//             continue;
//         }
//         int bitsLeft = i - pos.back() - 1;debug(bitsLeft,i ,"lol");
//         repp(_0, bitsLeft + 1){
//             int tot0 = c_zero + _0 + 1;
//             ll ways = ncr[bitsLeft][_0];
//             ans += ways * (get(tot0) - 1) * pw[pos.back() + 1];
//         }
//         if(pos.back() != -1) cnt[pos.back()] += get(bitsLeft);
//     }
//     ans += get(c_zero) - 1;
//     debug(ps,ans);
//     return ans;
// }
// 100 // 10 // 11

void pre(){
    repp(i,34) repp(j,34) {
        if(j > i) ncr[i][j] = 0;
        else if(j == i || j == 0) ncr[i][j] = 1;
    }
    rep(i,2,34){
        rep(j,1,i) ncr[i][j] = ncr[i - 1][j] + ncr[i - 1][j - 1];
    }
    pw[0] = 1;
    rep(i,1,34) pw[i] = pw[i - 1] * 3;
}

signed main()
{
    ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    pre();
    tc{
        int l , r;
        cin >> l >> r;
        if(l == r) {
            W(0,nl);
            continue;
        }
        while(1){
            int x1 = mylog(l,2) , x2 = mylog(r,2);
            if(x1 == x2) l -= (1 << x1) , r -= (1 << x2);
            else break;
        }
        int pos1 = mylog(l,2) , pos2 = mylog(r,2);
        ll ans = 0;
        rep(i,pos1 + 1, pos2) ans += solve1(l,i);
        ans += solve(l,r);
        W(ans << 1 , nl);
    }
}


// Bismillahir Rahmanir Rahim
#include <bits/stdc++.h>

using namespace std;

#define double long double
#define Long long long
#define EPS 1e-12
#define PI acos(-1.0)

#define MAX 3000000
#define MOD 1000000007

#define POS_INF (1LL << 55)
#define NEG_INF (-(1LL << 55))

#define RIGHT_MOST_INDEX (1 << 30)
#define LEFT_MOST_INDEX (-(1 << 30))

#define MEMSET(x,y) memset(x, y, sizeof(x))
#define PB push_back
#define MP make_pair
#define SORT(A) sort(A.begin(), A.end());
#define REVERSE(A) reverse(A.begin(), A.end());

string aa;
vector<Long> VEC, VECC;
Long PRECOMP[2000][2000], THR = 200;

Long solve(Long number, Long range)
{
    int siz, i, tot;
    Long ans, val;
    VEC.clear();
    VECC.clear();

    siz = 34;
    tot = 0;
    for(i=0; i<siz; i++)
    {
        VECC.PB((number & 1));
        VEC.PB((range & 1));
        if(!(number & 1)) tot++;

        number >>= 1;
        range >>= 1;
    }

    REVERSE(VEC);
    REVERSE(VECC);

    ans = 0;
    for(i=0; i<siz; i++)
    {
        if(VECC[i] == 0) tot--;
        if(VEC[i])
        {
            val = (1LL << tot);
            ans += val;
        }

        if(VEC[i] && VECC[i]) break;
        if(i == siz-1) ans++;
    }

    return ans;
}

Long check(Long l, Long r)
{
    if(l <= THR && r <= THR) return PRECOMP[l][r];
    if(l == r) return 0;
    if(l == 0) return (2*r + 1 + check(1, r));
    //if(l % 2 == 0 && r % 2 == 0) return (2 * check(l/2, r/2) + check(l/2, r/2 - 1));
    if(l % 2 == 0 && r % 2 == 0) return (3 * check(l/2, r/2) - 2 * (solve(r+1, r) - solve(r+1, l-1)));

    if(l % 2) return (check(l+1, r) + 2 * (solve(l, r) - solve(l, l-1)));
    if(r % 2) return (check(l, r-1) + 2 * (solve(r, r) - solve(r, l-1)));
}

int main()
{
    Long i,j,k,l,n,m,tot,temp,curr,val,prev,a,b,c,t,ans,flag,x,y,siz,ii,jj,xx,yy,root,ind,len,q,xxx,yyy,d,ll,rr,r;

    for(i=0; i<=THR; i++)
    {
        for(j=i; j<=THR; j++)
        {
            for(k=i; k<=j; k++)
            {
                for(l=i; l<=j; l++)
                {
                    if((k ^ l) == (k + l)) PRECOMP[i][j]++;
                }
            }
        }
    }

    cin>>q;
    //q = 100;
    while(q--)
    {
        cin>>l>>r;
        //l = 0;
        //r = 990607689;
        cout<<check(l, r)<<endl;
    }
}


// Bismillahir Rahmanir Rahim
#include <bits/stdc++.h>

using namespace std;

#define double long double
#define Long long long
#define EPS 1e-12
#define PI acos(-1.0)

#define MAX 3000000
#define MOD 1000000007

#define POS_INF (1LL << 55)
#define NEG_INF (-(1LL << 55))

#define RIGHT_MOST_INDEX (1 << 30)
#define LEFT_MOST_INDEX (-(1 << 30))

#define MEMSET(x,y) memset(x, y, sizeof(x))
#define PB push_back
#define MP make_pair
#define SORT(A) sort(A.begin(), A.end());
#define REVERSE(A) reverse(A.begin(), A.end());

string aa;
vector<Long> VEC, VECC;
Long PRECOMP[2000][2000], THR = 200;

Long solve(Long number, Long range)
{
    int siz, i, tot;
    Long ans, val;
    VEC.clear();
    VECC.clear();

    siz = 34;
    tot = 0;
    for(i=0; i<siz; i++)
    {
        VECC.PB((number & 1));
        VEC.PB((range & 1));
        if(!(number & 1)) tot++;

        number >>= 1;
        range >>= 1;
    }

    REVERSE(VEC);
    REVERSE(VECC);

    ans = 0;
    for(i=0; i<siz; i++)
    {
        if(VECC[i] == 0) tot--;
        if(VEC[i])
        {
            val = (1LL << tot);
            ans += val;
        }

        if(VEC[i] && VECC[i]) break;
        if(i == siz-1) ans++;
    }

    return ans;
}

Long check(Long l, Long r)
{
    if(l <= THR && r <= THR) return PRECOMP[l][r];
    if(l == r) return 0;
    if(l == 0) return (2*r + 1 + check(1, r));
    if(l % 2 == 0 && r % 2 == 0) return (2 * check(l/2, r/2) + check(l/2, r/2 - 1));

    if(l % 2) return (check(l+1, r) + 2 * (solve(l, r) - solve(l, l-1)));
    if(r % 2) return (check(l, r-1) + 2 * (solve(r, r) - solve(r, l-1)));
}

int main()
{
    Long i,j,k,l,n,m,tot,temp,curr,val,prev,a,b,c,t,ans,flag,x,y,siz,ii,jj,xx,yy,root,ind,len,q,xxx,yyy,d,ll,rr,r;

    for(i=0; i<=THR; i++)
    {
        for(j=i; j<=THR; j++)
        {
            for(k=i; k<=j; k++)
            {
                for(l=i; l<=j; l++)
                {
                    if((k ^ l) == (k + l)) PRECOMP[i][j]++;
                }
            }
        }
    }

    cin>>q;
    while(q--)
    {
        cin>>l>>r;
        cout<<check(l, r)<<endl;
    }
}


// Bismillahir Rahmanir Rahim
#include <bits/stdc++.h>

using namespace std;

#define double long double
#define Long long long
#define EPS 1e-12
#define PI acos(-1.0)

#define MAX 3000000
#define MOD 1000000007

#define POS_INF (1LL << 55)
#define NEG_INF (-(1LL << 55))

#define RIGHT_MOST_INDEX (1 << 30)
#define LEFT_MOST_INDEX (-(1 << 30))

#define MEMSET(x,y) memset(x, y, sizeof(x))
#define PB push_back
#define MP make_pair
#define SORT(A) sort(A.begin(), A.end());
#define REVERSE(A) reverse(A.begin(), A.end());

string aa;
vector<Long> VEC, VECC;
Long PRECOMP[2000][2000], THR = 200;

Long solve(Long number, Long range)
{
    int siz, i, tot;
    Long ans, val;
    VEC.clear();
    VECC.clear();

    siz = 34;
    tot = 0;
    for(i=0; i<siz; i++)
    {
        VECC.PB((number & 1));
        VEC.PB((range & 1));
        if(!(number & 1)) tot++;

        number >>= 1;
        range >>= 1;
    }

    REVERSE(VEC);
    REVERSE(VECC);

    ans = 0;
    for(i=0; i<siz; i++)
    {
        if(VECC[i] == 0) tot--;
        if(VEC[i])
        {
            val = (1LL << tot);
            ans += val;
        }

        if(VEC[i] && VECC[i]) break;
        if(i == siz-1) ans++;
    }

    return ans;
}

Long check(Long l, Long r)
{
    if(l <= THR && r <= THR) return PRECOMP[l][r];
    if(l == r) return 0;
    if(l == 0) return (2*r + 1 + check(1, r));
    if(l % 2 == 0 && r % 2 == 0) return (2 * check(l/2, r/2) + check(l/2, r/2 - 1));
    if(l % 2 == 0 && r % 2 == 0) return (3 * check(l/2, r/2) - 2 * (solve(r+1, r) - solve(r+1, l-1)));

    if(l % 2) return (check(l+1, r) + 2 * (solve(l, r) - solve(l, l-1)));
    if(r % 2) return (check(l, r-1) + 2 * (solve(r, r) - solve(r, l-1)));
}

int main()
{
    Long i,j,k,l,n,m,tot,temp,curr,val,prev,a,b,c,t,ans,flag,x,y,siz,ii,jj,xx,yy,root,ind,len,q,xxx,yyy,d,ll,rr,r;

    for(i=0; i<=THR; i++)
    {
        for(j=i; j<=THR; j++)
        {
            for(k=i; k<=j; k++)
            {
                for(l=i; l<=j; l++)
                {
                    if((k ^ l) == (k + l)) PRECOMP[i][j]++;
                }
            }
        }
    }

    cin>>q;
    //q = 100;
    while(q--)
    {
        cin>>l>>r;
        //l = 0;
        //r = 990607689;
        cout<<check(l, r)<<endl;
    }
}


// Bismillahir Rahmanir Rahim
#include <bits/stdc++.h>

using namespace std;

#define double long double
#define Long long long
#define EPS 1e-12
#define PI acos(-1.0)

#define MAX 3000000
#define MOD 1000000007

#define POS_INF (1LL << 55)
#define NEG_INF (-(1LL << 55))

#define RIGHT_MOST_INDEX (1 << 30)
#define LEFT_MOST_INDEX (-(1 << 30))

#define MEMSET(x,y) memset(x, y, sizeof(x))
#define PB push_back
#define MP make_pair
#define SORT(A) sort(A.begin(), A.end());
#define REVERSE(A) reverse(A.begin(), A.end());

string aa;
vector<Long> VEC, VECC;
Long PRECOMP[2000][2000], THR = 200;

Long solve(Long number, Long range)
{
    int siz, i, tot;
    Long ans, val;
    VEC.clear();
    VECC.clear();

    siz = 34;
    tot = 0;
    for(i=0; i<siz; i++)
    {
        VECC.PB((number & 1));
        VEC.PB((range & 1));
        if(!(number & 1)) tot++;

        number >>= 1;
        range >>= 1;
    }

    REVERSE(VEC);
    REVERSE(VECC);

    ans = 0;
    for(i=0; i<siz; i++)
    {
        if(VECC[i] == 0) tot--;
        if(VEC[i])
        {
            val = (1LL << tot);
            ans += val;
        }

        if(VEC[i] && VECC[i]) break;
        if(i == siz-1) ans++;
    }

    return ans;
}

Long check(Long l, Long r)
{
    if(l <= THR && r <= THR) return PRECOMP[l][r];
    if(l == r) return 0;
    if(l == 0) return (2*r + 1 + check(1, r));
    if(l % 2 == 0 && r % 2 == 0) return (3 * check(l/2, r/2) - 2 * (solve(r+1, r) - solve(r+1, l-1)));

    if(l % 2) return (check(l+1, r) + 2 * (solve(l, r) - solve(l, l-1)));
    if(r % 2) return (check(l, r-1) + 2 * (solve(r, r) - solve(r, l-1)));
}

int main()
{
    Long i,j,k,l,n,m,tot,temp,curr,val,prev,a,b,c,t,ans,flag,x,y,siz,ii,jj,xx,yy,root,ind,len,q,xxx,yyy,d,ll,rr,r;

    for(i=0; i<=THR; i++)
    {
        for(j=i; j<=THR; j++)
        {
            for(k=i; k<=j; k++)
            {
                for(l=i; l<=j; l++)
                {
                    if((k ^ l) == (k + l)) PRECOMP[i][j]++;
                }
            }
        }
    }

    cin>>q;
    while(q--)
    {
        cin>>l>>r;
        cout<<check(l, r)<<endl;
    }
}


// Bismillahir Rahmanir Rahim
#include <bits/stdc++.h>

using namespace std;

#define double long double
#define Long long long
#define EPS 1e-12
#define PI acos(-1.0)

#define MAX 3000000
#define MOD 1000000007

#define POS_INF (1LL << 55)
#define NEG_INF (-(1LL << 55))

#define RIGHT_MOST_INDEX (1 << 30)
#define LEFT_MOST_INDEX (-(1 << 30))

#define MEMSET(x,y) memset(x, y, sizeof(x))
#define PB push_back
#define MP make_pair
#define SORT(A) sort(A.begin(), A.end());
#define REVERSE(A) reverse(A.begin(), A.end());

string aa;
vector<Long> VEC, VECC;
Long PRECOMP[203][203], THR = 200;

Long solve(Long number, Long range)
{
    int siz, i, tot;
    Long ans, val;
    VEC.clear();
    VECC.clear();

    siz = 34;
    tot = 0;
    for(i=0; i<siz; i++)
    {
        VECC.PB((number & 1));
        VEC.PB((range & 1));
        if(!(number & 1)) tot++;

        number >>= 1;
        range >>= 1;
    }

    REVERSE(VEC);
    REVERSE(VECC);

    ans = 0;
    for(i=0; i<siz; i++)
    {
        if(VECC[i] == 0) tot--;
        if(VEC[i])
        {
            val = (1LL << tot);
            ans += val;
        }

        if(VEC[i] && VECC[i]) break;
        if(i == siz-1) ans++;
    }

    return ans;
}

Long check(Long l, Long r)
{
    if(l <= THR && r <= THR) return PRECOMP[l][r];
    if(l == r) return 0;
    if(l == 0) return (2*r + 1 + check(1, r));
    if(l % 2 == 0 && r % 2 == 0) return (3 * check(l/2, r/2) - 2 * (solve(r+1, r) - solve(r+1, l-1)));

    if(l % 2) return (check(l+1, r) + 2 * (solve(l, r) - solve(l, l-1)));
    if(r % 2) return (check(l, r-1) + 2 * (solve(r, r) - solve(r, l-1)));
}

int main()
{
    Long i,j,k,l,n,m,tot,temp,curr,val,prev,a,b,c,t,ans,flag,x,y,siz,ii,jj,xx,yy,root,ind,len,q,xxx,yyy,d,ll,rr,r;

    for(i=0; i<=THR; i++)
    {
        for(j=i; j<=THR; j++)
        {
            for(k=i; k<=j; k++)
            {
                for(l=i; l<=j; l++)
                {
                    if((k ^ l) == (k + l)) PRECOMP[i][j]++;
                }
            }
        }
    }

    cin>>q;
    while(q--)
    {
        cin>>l>>r;
        cout<<check(l, r)<<endl;
    }
}


// Bismillahir Rahmanir Rahim
#include <bits/stdc++.h>

using namespace std;

#define double long double
#define Long long long
#define EPS 1e-12
#define PI acos(-1.0)

#define MAX 3000000
#define MOD 1000000007

#define POS_INF (1LL << 55)
#define NEG_INF (-(1LL << 55))

#define RIGHT_MOST_INDEX (1 << 30)
#define LEFT_MOST_INDEX (-(1 << 30))

#define MEMSET(x,y) memset(x, y, sizeof(x))
#define PB push_back
#define MP make_pair
#define SORT(A) sort(A.begin(), A.end());
#define REVERSE(A) reverse(A.begin(), A.end());

string aa;
vector<Long> VEC, VECC;
Long PRECOMP[200][200];

Long solve(Long number, Long range)
{
    int siz, i, tot;
    Long ans, val;
    VEC.clear();
    VECC.clear();

    //cout<<"new: "<<number<<" "<<range<<" "<<endl;

    siz = 40;
    tot = 0;
    for(i=0; i<siz; i++)
    {
        VECC.PB(number % 2);
        VEC.PB(range % 2);

        if(number % 2 == 0) tot++;

        number /= 2;
        range /= 2;
    }

    REVERSE(VEC);
    REVERSE(VECC);

    ans = 0;
    for(i=0; i<siz; i++)
    {
        if(VECC[i] == 0) tot--;
        if(VEC[i])
        {
            val = (1LL << tot);
            ans += val;
        }

        if(VEC[i] && VECC[i]) break;
        if(i == siz-1) ans++;
    }

    //cout<<ans<<endl;
    return ans;
}

Long check(Long l, Long r)
{
    //if(l <= 2 && r <= 2) return PRECOMP[l][r];
    if(l <= 50 && r <= 50) return PRECOMP[l][r];
    if(l == r) return 0;
    if(l == 0) return (2*r + 1 + check(1, r));
    if(l % 2 == 0 && r % 2 == 0) return (2 * check(l/2, r/2) + check(l/2, r/2 - 1));

    if(l % 2) return (check(l+1, r) + 2 * (solve(l, r) - solve(l, l-1)));
    if(r % 2) return (check(l, r-1) + 2 * (solve(r, r) - solve(r, l-1)));
}

int main()
{
    Long i,j,k,l,n,m,tot,temp,curr,val,prev,a,b,c,t,ans,flag,x,y,siz,ii,jj,xx,yy,root,ind,len,q,xxx,yyy,d,ll,rr,r;

    for(i=0; i<=50; i++)
    {
        for(j=i; j<=50; j++)
        {
            for(k=i; k<=j; k++)
            {
                for(l=i; l<=j; l++)
                {
                    if((k ^ l) == (k + l)) PRECOMP[i][j]++;
                }
            }
        }
    }

    cin>>q;
    while(q--)
    {
        cin>>l>>r;
        cout<<check(l, r)<<endl;

//        ll = l;
//        rr = r;
//        Long main_ans = 0;
//
//        VEC.clear();
//        while(r)
//        {
//            VEC.PB(r % 2);
//            r /= 2;
//        }
//
//        REVERSE(VEC);
//        siz = VEC.size();
//
//        ans = 0;
//        temp = 0;
//        for(i=0; i<siz; i++)
//        {
//            if(VEC[i])
//            {
//                if(i)
//                {
//                    val = (i + 1 - temp);
//                    val = (1LL << val);
//                    val %= MOD;
//                }
//                else val = 1;
//
//                val *= POW[siz - (i+1)];
//                val %= MOD;
//
//                ans += val;
//                ans %= MOD;
//
//                temp++;
//            }
//        }
//
//        if(VEC[siz-1])
//        {
//            val = (siz - temp);
//            val = (1LL << val);
//            val %= MOD;
//
//            ans += val;
//            ans %= MOD;
//        }
//
//        main_ans += ans;
//
//        VEC.clear();
//        while(l)
//        {
//            VEC.PB(l % 2);
//            l /= 2;
//        }
//
//        REVERSE(VEC);
//        siz = VEC.size();
//
//        ans = 0;
//        temp = 0;
//        for(i=0; i<siz; i++)
//        {
//            if(VEC[i])
//            {
//                if(i)
//                {
//                    val = (i + 1 - temp);
//                    val = (1LL << val);
//                    val %= MOD;
//                }
//                else val = 1;
//
//                val *= POW[siz - (i+1)];
//                val %= MOD;
//
//                ans += val;
//                ans %= MOD;
//
//                temp++;
//            }
//        }
//
//        if(VEC[siz-1])
//        {
//            val = (siz - temp);
//            val = (1LL << val);
//            val %= MOD;
//
//            ans += val;
//            ans %= MOD;
//        }
//
//        main_ans += ans;
    }
}


/**
* Number:cf1245f
* Title:Daniel and Spring Cleaning
* Status:?
* Tag:[]
**/

#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <numeric>
#include <vector>
#include <queue>
#include <cassert>
using namespace std;

#define INF 0x3f3f3f3f
typedef long long ll;
typedef unsigned long long ull;

inline int readi(int& i1) { return scanf("%d", &i1); }
inline int readi(int& i1, int& i2) { return scanf("%d %d", &i1, &i2); }
inline int readi(int& i1, int& i2, int& i3) { return scanf("%d %d %d", &i1, &i2, &i3); }
inline int readi(int& i1, int& i2, int& i3, int& i4) { return scanf("%d %d %d %d", &i1, &i2, &i3, &i4); }
inline int reads(char* s1) { return scanf("%s", s1); }
#define clr(mem, val) memset(mem, val, sizeof(mem))
#define rep(i, begin, end) for (register int i = (begin); i <= (end); i++)
#define rep2(i1, begin1, end1, i2, begin2, end2) rep(i1, begin1, end1) rep(i2, begin2, end2)
#define repne(i, begin, end) for (register int i = (begin); i < (end); i++)
#define repne2(i1, begin1, end1, i2, begin2, end2) repne(i1, begin1, end1) repne(i2, begin2, end2)

ll dp[40];
ll dfs(int x,int y,int k,bool boundx,bool boundy)
{
    if(x==-1 || y==-1)return 0;
    if(k==-1)return 1;
    if(!boundx && !boundy && dp[k]!=-1)return dp[k];
    int limx=boundx?(x>>k)&1:1, limy=boundy?(y>>k)&1:1;
    ll ans=0;
    for(int i=0;i<=limx;i++)
    {
        for(int j=0;j<=limy;j++)
        {
            if((i&j)==0)
            {
                ans+=dfs(x,y,k-1,boundx&&i==limx,boundy&&j==limy);
            }
        }
    }
    if(!boundx && !boundy)dp[k]=ans;
    return ans;
}
int main()
{
#ifdef __DEBUG__
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif
    int T; readi(T);
    clr(dp,-1);
    while(T--)
    {
        int l,r; readi(l,r);
        ll ans=
            dfs(r,r,30,true,true)
            -dfs(l-1,r,30,true,true)
            -dfs(r,l-1,30,true,true)
            +dfs(l-1,l-1,30,true,true);
        printf("%lld\n",ans);
    }
    return 0;
}


/**
* Number:cf1245f
* Title:Daniel and Spring Cleaning
* Status:?
* Tag:[]
**/

#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <numeric>
#include <vector>
#include <queue>
#include <cassert>
using namespace std;

#define INF 0x3f3f3f3f
typedef long long ll;
typedef unsigned long long ull;

inline int readi(int& i1) { return scanf("%d", &i1); }
inline int readi(int& i1, int& i2) { return scanf("%d %d", &i1, &i2); }
inline int readi(int& i1, int& i2, int& i3) { return scanf("%d %d %d", &i1, &i2, &i3); }
inline int readi(int& i1, int& i2, int& i3, int& i4) { return scanf("%d %d %d %d", &i1, &i2, &i3, &i4); }
inline int reads(char* s1) { return scanf("%s", s1); }
#define clr(mem, val) memset(mem, val, sizeof(mem))
#define rep(i, begin, end) for (register int i = (begin); i <= (end); i++)
#define rep2(i1, begin1, end1, i2, begin2, end2) rep(i1, begin1, end1) rep(i2, begin2, end2)
#define repne(i, begin, end) for (register int i = (begin); i < (end); i++)
#define repne2(i1, begin1, end1, i2, begin2, end2) repne(i1, begin1, end1) repne(i2, begin2, end2)

ll dp[40][2][2];
ll dfs(int x,int y,int k,int boundx,int boundy)
{
    if(x==-1 || y==-1)return 0;
    if(k==-1)return 1;
    if(dp[k][boundx][boundy]!=-1)return dp[k][boundx][boundy];
    int limx=boundx?(x>>k)&1:1, limy=boundy?(y>>k)&1:1;
    ll ans=0;
    for(int i=0;i<=limx;i++)
    {
        for(int j=0;j<=limy;j++)
        {
            if((i&j)==0)
            {
                ans+=dfs(x,y,k-1,boundx&&i==limx,boundy&&j==limy);
            }
        }
    }
    return dp[k][boundx][boundy]=ans;
}
ll solve(int l,int r)
{
    clr(dp,-1);
    ll ans=dfs(l,r,30,true,true);
    return ans;
}
int main()
{
#ifdef __DEBUG__
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif
    int T; readi(T);
    while(T--)
    {
        int l,r; readi(l,r);
        ll ans=
            solve(r,r)
            -solve(l-1,r)
            -solve(r,l-1)
            +solve(l-1,l-1);
        printf("%lld\n",ans);
    }
    return 0;
}


#pragma GCC target("avx2")
#pragma GCC optimize("O3")

# include <x86intrin.h>
# include <bits/stdc++.h>

# include <ext/pb_ds/assoc_container.hpp>
# include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;

template<typename T> using ordered_set = tree <T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define _USE_MATH_DEFINES_
#define ll long long
#define ld long double
#define Accepted 0
#define pb push_back
#define mp make_pair
#define sz(x) (int)(x.size())
#define every(x) x.begin(),x.end()
#define F first
#define S second
#define lb lower_bound
#define ub upper_bound
#define For(i,x,y)  for (ll i = x; i <= y; i ++)
#define FOr(i,x,y)  for (ll i = x; i >= y; i --)
#define SpeedForce ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
// ROAD to...                                                                                Red

inline void Input_Output () {
	//freopen(".in", "r", stdin);
   //freopen(".out", "w", stdout);
}

const double eps = 0.000001;
const ld pi = acos(-1);
const int maxn = 1e7 + 9;
const int mod = 1e9 + 7;
const ll MOD = 1e18 + 9;
const ll INF = 1e18 + 123;
const int inf = 2e9 + 11;
const int mxn = 1e6 + 9;
const int N = 6e5 + 123;
const int M = 30;
const int pri = 997;
const int Magic = 2101;

const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};
vector < int > L, R;
ll dp[M][2][2][2][2];

ll go (int bit, int ax, int ay, int bx, int by) {
	if (bit == M) return 1;
	ll &res = dp[bit][ax][ay][bx][by];
	//cout << res
	if (res != -1) return res;
	//cout << bit << ' ' << ax << ' ' << ay << ' ' << bx << ' ' << by << '\n';

	res = 0;
	//0 0
	if (L[bit] == 0 || (ax == 0 && bx == 0))
		res += go(bit + 1, (ax && L[bit] == 0), (ay && R[bit] == 0), (bx && L[bit] == 0), (by && R[bit] == 0));
	//0 1
	if ((ax == 0 || L[bit] == 0) && (by == 0 || R[bit] == 1))
		res += go(bit + 1, (ax && L[bit] == 0), (ay && R[bit] == 0), (bx && L[bit] == 1), (by && R[bit] == 1));
	//1 0
	if ((ay == 0 || R[bit] == 1) && (bx == 0 || L[bit] == 0))
		res += go(bit + 1, (ax && L[bit] == 1), (ay && R[bit] == 1), (bx && L[bit] == 0), (by && R[bit] == 0));

//	if(res > 0)
//		cout << bit << ' ' << ax << ' ' << ay << ' ' << bx << ' ' << by << '\n';

	return res;
}

inline ll get (int l, int r) {
	L.clear();
	R.clear();

	for (int i = M - 1; i >= 0; i --) {
		if (l & (1 << i)) L.pb(1);
		else L.pb(0);

		if (r & (1 << i)) R.pb(1);
		else R.pb(0);
	}
	//cout << sz(L) << ' ' << sz(R) << '\n';
	for (int i = 0; i < M; i ++)
		for (int a = 0; a < 2; a++)
			for (int b = 0; b < 2; b++)
				for (int c = 0; c < 2; c ++)
					for (int d = 0; d < 2; d++)
						dp[i][a][b][c][d] = -1;

	return go(0, 1, 1, 1, 1);
}

int main () {
   int t;
	scanf("%d", &t);
	while(t--) {
		int l, r;
		scanf("%d%d", &l, &r);
		printf("%lld\n", get(l, r));
	}
   return Accepted;
}

// B...a


#pragma GCC target("avx2")
#pragma GCC optimize("O3")

# include <x86intrin.h>
# include <bits/stdc++.h>

# include <ext/pb_ds/assoc_container.hpp>
# include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;

template<typename T> using ordered_set = tree <T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define _USE_MATH_DEFINES_
#define ll long long
#define ld long double
#define Accepted 0
#define pb push_back
#define mp make_pair
#define sz(x) (int)(x.size())
#define every(x) x.begin(),x.end()
#define F first
#define S second
#define lb lower_bound
#define ub upper_bound
#define For(i,x,y)  for (ll i = x; i <= y; i ++)
#define FOr(i,x,y)  for (ll i = x; i >= y; i --)
#define SpeedForce ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
// ROAD to...                                                                                Red

inline void Input_Output () {
	//freopen(".in", "r", stdin);
   //freopen(".out", "w", stdout);
}

const double eps = 0.000001;
const ld pi = acos(-1);
const int maxn = 1e7 + 9;
const int mod = 1e9 + 7;
const ll MOD = 1e18 + 9;
const ll INF = 1e18 + 123;
const int inf = 2e9 + 11;
const int mxn = 1e6 + 9;
const int N = 6e5 + 123;
const int M = 30;
const int pri = 997;
const int Magic = 2101;

const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};
vector < int > L, R;
ll dp[M][2][2];

ll go (int bit, int ax, int ay) {
	if (bit == M) return 1;
	ll &res = dp[bit][ax][ay];
	//cout << res
	if (res != -1) return res;
	//cout << bit << ' ' << ax << ' ' << ay << ' ' << bx << ' ' << by << '\n';

	res = 0;
	int h1 = L[bit];
	int h2 = R[bit];

	if (ax) h1 = 1;
	if (ay) h2 = 1;

	for (int i = 0; i <= h1; i ++)
		for (int j = 0; j <= h2; j ++) {
			if (i & j) continue;
			res += go(bit + 1, ax | (i < L[bit]), ay | (j < R[bit]));
		}

	return res;
}

inline ll get (int l, int r) {
	if (l < 0) return 0;
	L.clear();
	R.clear();

	for (int i = M - 1; i >= 0; i --) {
		if (l & (1 << i)) L.pb(1);
		else L.pb(0);

		if (r & (1 << i)) R.pb(1);
		else R.pb(0);
	}
	//cout << sz(L) << ' ' << sz(R) << '\n';
	for (int i = 0; i < M; i ++)
		for (int a = 0; a < 2; a++)
			for (int b = 0; b < 2; b++)
				dp[i][a][b] = -1;

	return go(0, 0, 0);
}

int main () {
   int t;
	scanf("%d", &t);
	while(t--) {
		int l, r;
		scanf("%d%d", &l, &r);
		--l;
		printf("%lld\n", get(r, r) - 2 * get(l, r) + get(l, l));
	}
   return Accepted;
}

// B...a


#pragma GCC target("avx2")
#pragma GCC optimize("O3")

# include <x86intrin.h>
# include <bits/stdc++.h>

# include <ext/pb_ds/assoc_container.hpp>
# include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;

template<typename T> using ordered_set = tree <T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define _USE_MATH_DEFINES_
#define ll long long
#define ld long double
#define Accepted 0
#define pb push_back
#define mp make_pair
#define sz(x) (int)(x.size())
#define every(x) x.begin(),x.end()
#define F first
#define S second
#define lb lower_bound
#define ub upper_bound
#define For(i,x,y)  for (ll i = x; i <= y; i ++)
#define FOr(i,x,y)  for (ll i = x; i >= y; i --)
#define SpeedForce ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
// ROAD to...                                                                                Red

inline void Input_Output () {
	//freopen(".in", "r", stdin);
   //freopen(".out", "w", stdout);
}

const double eps = 0.000001;
const ld pi = acos(-1);
const int maxn = 1e7 + 9;
const int mod = 1e9 + 7;
const ll MOD = 1e18 + 9;
const ll INF = 1e18 + 123;
const int inf = 2e9 + 11;
const int mxn = 1e6 + 9;
const int N = 6e5 + 123;
const int M = 22;
const int pri = 997;
const int Magic = 2101;

const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};

vector < pair < int, int > > b;
ll f[N];
int cur = 0;
inline void go (int l, int r, int lim, ll sum = 0) {
	if (sum > lim) return ;
	if (l == r) {
	   if (r == 16) {
			f[sum] += (1 << cur);
			return ;
		}
		b.pb({sum, (1 << cur)});
		return ;
	}

	go (l + 1, r, lim, sum);
	cur++;
	go (l + 1, r, lim, sum + (1ll << l));
	cur--;
}

inline ll get (int l, int r) {
	for (int i = 0; i < (1 << 16); i ++) f[i] = 0;
	b.clear();
	go	(0, 16, r);
	go (16, 31, r);
	for (int i = 1; i < (1 << 16); i ++) f[i] += f[i - 1];

	ll res = 0;
	for (auto y : b) if (r >= y.F) {
		int now = min({r, (1 << 16) - 1, r - y.F});
		//cout << x - y << "kek: ";
	//	cout << "add: "<< cnt[y] << ' ' << y << ' ' << now << ' ' << f[now] << '\n';
		res += f[now] * y.S;
	}
	for (auto y : b) if (l >= y.F) {
		int now = min({l, (1 << 16) - 1, l - y.F});
		//cout << x - y << "kek: ";
	//	cout << "reM: " << cnt[y] << ' ' << y << ' ' << now << ' ' << f[now] << '\n';
		res -= f[now] * y.S;
	}

	return res;

}

int main () {
   int t;
	scanf("%d", &t);
	while(t--) {
		int l, r;
		scanf("%d%d", &l, &r);
		printf("%lld\n", get(l, r));
	}
   return Accepted;
}

// B...a


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll dp[40][2][2][2][2];
int abit[33],bbit[33];
ll dfs(int len,int lim1,int lim2,int z1,int z2){
    if(len==-1)return 1;
    if(!lim1&&!lim2&&dp[len][lim1][lim2][z1][z2])return dp[len][lim1][lim2][z1][z2];
    ll ans=0;
    int up1=lim1?abit[len]:1;
    int up2=lim2?bbit[len]:1;
    for(int i=0;i<=up1;i++){
        for(int j=0;j<=up2;j++){
            if((i+j)==(i^j)){
                ans+=dfs(len-1,lim1&&i==up1,lim2&&j==up2,z1||i!=0,z2||j!=0);
            }
        }
    }
    if(!lim1&&!lim2){
        dp[len][lim1][lim2][z1][z2]=ans;
    }
    return ans;
}
ll slove(ll a,ll b){
    memset(dp,0, sizeof(dp));
    for(int i=30;i>=0;i--){
        abit[i]=a>>i&1;
        bbit[i]=b>>i&1;
    }
    dfs(30,1,1,0,0);
}
ll t,l,r;
int main(){
    ios::sync_with_stdio(false);
    cin>>t;
    ll a,b,c;
    while (t--){
        cin>>l>>r;
//        cout<<(a=slove(r,r))<<' ';
//        cout<<(b=slove(l-1,l-1))<<' ';
//        cout<<(c=slove(l-1,r))<<' ';
//        cout<<endl;
        a=slove(r,r);
        b=slove(l-1,l-1);
        c=slove(l-1,r);
        cout<<a+b-c-c<<endl;
    }
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll dp[40][2][2][2][2];
int abit[33],bbit[33];
ll dfs(int len,int lim1,int lim2,int z1,int z2){
    if(len==-1)return 1;
    if(dp[len][lim1][lim2][z1][z2]!=-1)return dp[len][lim1][lim2][z1][z2];
    ll ans=0;
    int up1=lim1?abit[len]:1;
    int up2=lim2?bbit[len]:1;
    for(int i=0;i<=up1;i++){
        for(int j=0;j<=up2;j++){
            if((i+j)==(i^j)){
                ans+=dfs(len-1,lim1&&i==up1,lim2&&j==up2,z1||i!=0,z2||j!=0);
            }
        }
    }
    dp[len][lim1][lim2][z1][z2]=ans;
    return ans;
}
ll slove(ll a,ll b){
    memset(dp,-1, sizeof(dp));
    for(int i=30;i>=0;i--){
        abit[i]=a>>i&1;
        bbit[i]=b>>i&1;
    }
    dfs(30,1,1,0,0);
}
ll t,l,r;
int main(){
    ios::sync_with_stdio(false);
    cin>>t;
    ll a,b,c;
    while (t--){
        cin>>l>>r;
        if(l==0){
            a=slove(r,r);
            cout<<a<<endl;
            continue;
        }
//        cout<<(a=slove(r,r))<<' ';
//        cout<<(b=slove(l-1,l-1))<<' ';
//        cout<<(c=slove(l-1,r))<<' ';
//        cout<<endl;
        a=slove(r,r);
        b=slove(l-1,l-1);
        c=slove(l-1,r);
        cout<<a+b-c-c<<endl;
    }
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll dp[40][2][2][2][2];
int abit[33],bbit[33];
ll dfs(int len,int lim1,int lim2,int z1,int z2){
    if(len==-1)return 1;
    if(dp[len][lim1][lim2][z1][z2]!=-1)return dp[len][lim1][lim2][z1][z2];
    ll ans=0;
    int up1=lim1?abit[len]:1;
    int up2=lim2?bbit[len]:1;
    for(int i=0;i<=up1;i++){
        for(int j=0;j<=up2;j++){
            if((i+j)==(i^j)){
                ans+=dfs(len-1,lim1&&i==up1,lim2&&j==up2,z1||i!=0,z2||j!=0);
            }
        }
    }
    dp[len][lim1][lim2][z1][z2]=ans;
    return ans;
}
ll slove(ll a,ll b){
    memset(dp,-1, sizeof(dp));
    for(int i=30;i>=0;i--){
        abit[i]=a>>i&1;
        bbit[i]=b>>i&1;
    }
    dfs(30,1,1,0,0);
}
ll t,l,r;
int main(){
    ios::sync_with_stdio(false);
    cin>>t;
    ll a,b,c;
    while (t--){
        cin>>l>>r;
//        cout<<(a=slove(r,r))<<' ';
//        cout<<(b=slove(l-1,l-1))<<' ';
//        cout<<(c=slove(l-1,r))<<' ';
//        cout<<endl;
        a=slove(r,r);
        b=slove(l-1,l-1);
        c=slove(l-1,r);
        cout<<a+b-c-c<<endl;
    }
}


#include<bits/stdc++.h>
#define ff first
#define ss second
#define pb push_back
#define pf push_front
#define eb emplace_back
#define emp emplace
#define ins insert
#define mp make_pair
#define mt make_tuple
#define sz(s) (int)s.size()
#define forp(i,a,b) for( int i=a;i<=b;i++)
#define rep(i,n)    for( int i=0;i<n;i++)
#define ren(i,n)    for( int i=n-1;i>=0;i--)
#define forn(i,a,b) for( int i=a;i>=b;i--)
#define w(t) while(t)
#define sc(a) scanf("%d", &a);
#define on cout<<"\n"
#define os cout <<" "
#define o2(a,b) cout<<a<<" "<<b
#define o(a) cout << a
#define ppr(a) cout << a.first << " " << a.second
#define bitcount __builtin_popcount
#define gcd __gcd
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(), v.rend()
#define mem(n,m) memset(n,m,sizeof(n))
#define pii pair<int,int>
#define tiii tuple<int, int, int>
#define pll pair<long long,long long>
#define sii set<int>
#define sll set<long long>
#define vii vector<int>
#define vll vector<long long>
#define mii map<int,int>
#define mll map<long long, long long>
#define lob lower_bound
#define upb upper_bound
#define ret return
#define present(s,x) (s.find(x) != s.end())
#define cpresent(s,x) (find(all(s),x) != s.end())
#define ford(container, it) for(auto it = container.begin(); it != container.end(); it++)
#define fors(container, it, a, b) for(auto it = a; it != b; it++)
#define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define MOD 1000000007
#define EPSILON 1e-9
#define PI acos(-1)
#define int long long
#define inf 1e15
#define debug(a) cerr<<#a<<": ";for(auto i:a)cerr<<i<<" ";cerr<<'\n';
#define trace(a) cerr<<#a<<": "<<a<<"\n"
//#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>

typedef long long  ll;
typedef long double  ldo;
typedef double  db ;
using namespace std;
auto start = std::chrono::system_clock::now();
inline void skj()
{
  std::chrono::time_point<std::chrono::system_clock> end;
  end = std::chrono::system_clock::now();
  std::chrono::duration<double> elapsed_seconds = end - start;
  std::time_t end_time = std::chrono::system_clock::to_time_t(end);
  cerr<<"Time taken " << elapsed_seconds.count()*1000<<"\n";
}

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//uniform_int_distribution<int> ud(1, 100); use this for random number generator

    //custom hash for unordered map
struct custom_hash {
    static uint64_t splitmix64(uint64_t x) {
        // http://xorshift.di.unimi.it/splitmix64.c
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};
inline int binexp(int a,int b,int m)
{
    if(a == 0) {
        ret 0;
    }
    int res=1;
    a%=m;
    while(b)
    {
        if(b&1)
            res=(res*1ll*a)%m;
        a=(a*1ll*a)%m;
        b>>=1;
    }
    return res;
}
inline int binmul(int a, int b, int m) {
    a %= m;
    int res = 0;
    w(b) {
        if(b & 1) {
            res = (res + a) % m;
        }
        a = (a + a) % m;
        b >>= 1;
    }
    ret res;
}
int dp[32][2][2][2];
int l, r;
inline int solve(int n, int f, int f1, int f2) {
    if(n == -1) ret 1;
    if(dp[n][f][f1][f2] != -1) ret dp[n][f][f1][f2];
    int ans = 0;
    rep(i, 2) {
        rep(j, 2) {
            if(f1 == 0 && (r & (1ll << n)) == 0 && i == 1) {
                continue;
            }

            if((f == 0 && i < j) || (i & j)) continue;
            if(f2 == 0 && (l & (1ll << n)) != 0 && j == 0) continue;
            ans += solve(n - 1, f | (i > j), f1 | ((r & (1ll << n)) != 0 && i == 0), f2 | ((l & (1ll << n)) == 0 && j == 1));
        }
    }
    ret dp[n][f][f1][f2] = ans;
}
inline void solve_me_senpai() {
    cin >> l >> r;
    mem(dp, -1);
    int ans = 2 * solve(31, 0, 0, 0);
    if(l == 0 and r == 0) ans = 1;
    else if(l == 0) ans--;
    o(ans);on;
}
int32_t main()
{
    boost
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
    int t = 1;
    cin >> t;
    int a = 1;
    while(t--) {
        //cout << "Case #" << a  << ": ";
        solve_me_senpai();
        a++;
    }
    //skj();
    return 0;
}


#include<bits/stdc++.h>
#define ff first
#define ss second
#define pb push_back
#define pf push_front
#define eb emplace_back
#define emp emplace
#define ins insert
#define mp make_pair
#define mt make_tuple
#define sz(s) (int)s.size()
#define forp(i,a,b) for( int i=a;i<=b;i++)
#define rep(i,n)    for( int i=0;i<n;i++)
#define ren(i,n)    for( int i=n-1;i>=0;i--)
#define forn(i,a,b) for( int i=a;i>=b;i--)
#define w(t) while(t)
#define sc(a) scanf("%d", &a);
#define on cout<<"\n"
#define os cout <<" "
#define o2(a,b) cout<<a<<" "<<b
#define o(a) cout << a
#define ppr(a) cout << a.first << " " << a.second
#define bitcount __builtin_popcount
#define gcd __gcd
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(), v.rend()
#define mem(n,m) memset(n,m,sizeof(n))
#define pii pair<int,int>
#define tiii tuple<int, int, int>
#define pll pair<long long,long long>
#define sii set<int>
#define sll set<long long>
#define vii vector<int>
#define vll vector<long long>
#define mii map<int,int>
#define mll map<long long, long long>
#define lob lower_bound
#define upb upper_bound
#define ret return
#define present(s,x) (s.find(x) != s.end())
#define cpresent(s,x) (find(all(s),x) != s.end())
#define ford(container, it) for(auto it = container.begin(); it != container.end(); it++)
#define fors(container, it, a, b) for(auto it = a; it != b; it++)
#define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define MOD 1000000007
#define EPSILON 1e-9
#define PI acos(-1)
#define int long long
#define inf 1e15
#define debug(a) cerr<<#a<<": ";for(auto i:a)cerr<<i<<" ";cerr<<'\n';
#define trace(a) cerr<<#a<<": "<<a<<"\n"
//#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>

typedef long long  ll;
typedef long double  ldo;
typedef double  db ;
using namespace std;
auto start = std::chrono::system_clock::now();
inline void skj()
{
  std::chrono::time_point<std::chrono::system_clock> end;
  end = std::chrono::system_clock::now();
  std::chrono::duration<double> elapsed_seconds = end - start;
  std::time_t end_time = std::chrono::system_clock::to_time_t(end);
  cerr<<"Time taken " << elapsed_seconds.count()*1000<<"\n";
}

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//uniform_int_distribution<int> ud(1, 100); use this for random number generator

    //custom hash for unordered map
struct custom_hash {
    static uint64_t splitmix64(uint64_t x) {
        // http://xorshift.di.unimi.it/splitmix64.c
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};
inline int binexp(int a,int b,int m)
{
    if(a == 0) {
        ret 0;
    }
    int res=1;
    a%=m;
    while(b)
    {
        if(b&1)
            res=(res*1ll*a)%m;
        a=(a*1ll*a)%m;
        b>>=1;
    }
    return res;
}
inline int binmul(int a, int b, int m) {
    a %= m;
    int res = 0;
    w(b) {
        if(b & 1) {
            res = (res + a) % m;
        }
        a = (a + a) % m;
        b >>= 1;
    }
    ret res;
}
int dp[32][2][2][2];
int l, r;
inline int solve(int n, int f, int f1, int f2) {
    if(n == -1) ret 1;
    if(dp[n][f][f1][f2] != -1) ret dp[n][f][f1][f2];
    int ans = 0;
    rep(i, 2) {
        rep(j, 2) {
            if(f1 == 0 && (r & (1ll << n)) == 0 && i == 1) {
                continue;
            }

            if((f == 0 && i < j) || (i & j)) continue;
            if(f2 == 0 && (l & (1ll << n)) != 0 && j == 0) continue;
            ans += solve(n - 1, f | (i > j), f1 | ((r & (1ll << n)) != 0 && i == 0), f2 | ((l & (1ll << n)) == 0 && j == 1));
        }
    }
    ret dp[n][f][f1][f2] = ans;
}
inline void solve_me_senpai() {
    cin >> l >> r;
    mem(dp, -1);
    o(2 * solve(31, 0, 0, 0));on;
}
int32_t main()
{
    boost
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
    int t = 1;
    cin >> t;
    int a = 1;
    while(t--) {
        //cout << "Case #" << a  << ": ";
        solve_me_senpai();
        a++;
    }
    //skj();
    return 0;
}


#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<bits/stdc++.h>
#include<stack>
#include<queue>
#include<list>
#include<vector>
#include<bitset>
#include<unordered_map>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
// #include "boost/algorithm/string.hpp"
#define fio ios_base::sync_with_stdio(false)
#define mod 1000000007
#define mod1 mod
#define mod2 1000000007
#define li long long int
#define ll li
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define readi(x) scanf("%d",&x)
#define  reads(x)  scanf("%s", x)
#define readl(x) scanf("%I64d",&x)
#define rep(i,n) for(i=0;i<n;i++)
#define revp(i,n) for(i=(n-1);i>=0;i--)
#define myrep1(i,a,b) for(i=a;i<=b;i++)
#define myrep2(i,a,b) for(i=b;i>=a;i--)
#define pb push_back
#define mp make_pair
#define fi first
#define sec second
#define MAXN 1000000000000000005
#define MINN -1000000000000000000
#define INTMAX 1000001000
#define pii pair<ll,ll>
#define pdd pair<double,double>
#define pic pair<int,char>
#define N 4005
#define lgn 20
#define ddouble long double
#define minus minu
#define PI 3.1415926535
#define lgn 20

// #define INTMAX 200000000

// using namespace boost;
// #define si short int

using namespace std;
using namespace __gnu_pbds;
typedef priority_queue<ll, vector<ll > > max_pq;
typedef priority_queue<ll, vector<ll> , greater<ll>  > min_pq;
ll toint(const string &s) {stringstream ss; ss << s; ll x; ss >> x; return x;}
string tostring ( ll number ){stringstream ss; ss<< number; return ss.str();}

// using namespace __gnu_pbds;
// typedef priority_queue<pair<ll,char> , vector<pair<ll , char> > > max_pq;
// typedef priority_queue<pii , vector<pii > ,greater<pii > > min_pq;
typedef tree<pii , null_type, less<pii>, rb_tree_tag, tree_order_statistics_node_update> OST;
// typedef priority_queue<pair < ll , pair < pii , ll > >  , vector<pair < ll , pair < pii , ll > >  > ,greater<pair < ll , pair < pii , ll > >  > > min_pq;
#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
    template <typename Arg1>
    void __f(const char* name, Arg1&& arg1){
        cout << name << " : " << arg1 << std::endl;
        //use cerr if u want to display at the bottom
    }
    template <typename Arg1, typename... Args>
    void __f(const char* names, Arg1&& arg1, Args&&... args){
        const char* comma = strchr(names + 1, ','); cout.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
    }
#else
#define trace(...)
#endif
#define nn 2000005
ll l,r,t;
ll dp[35][ 2 ][ 2 ][ 2 ][ 2 ];
ll solve ( ll pos, ll tl1, ll tr1, ll tl2, ll tr2 ){
    if ( pos == -1 ){
        return 1;
    }
    if ( dp[pos][tl1][tr1][tl2][tr2] != -1 ){
        return dp[pos][tl1][tr1][tl2][tr2];
    }
    ll lbit = ((1LL<<pos)&l)?1:0;
    ll rbit = ((1LL<<pos)&r)?1:0;
    ll sl = ((tl1)?lbit:0);
    ll el = ((tr1)?rbit:1);
    ll sr = ((tl2)?lbit:0);
    ll er = ((tr2)?rbit:1);
    ll ret = 0;
    for ( ll i = sl; i <= el; i ++){
        for ( ll j = sr; j <= er; j ++){
            if ( (i==0 or j == 0)){
                ret += solve(pos-1,tl1&(sl==i),tr1&(el==i),tl2&(sr==j),tr2&(er==j));
            }
        }
    }
    return dp[pos][tl1][tr1][tl2][tr2] = ret;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    #endif
    ll t;
    cin >> t;
    while ( t-- ){
        cin >> l >> r;
        memset(dp,-1,sizeof dp);
        cout << solve(31,1,1,1,1) <<"\n";
    }
}


#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<bits/stdc++.h>
#include<stack>
#include<queue>
#include<list>
#include<vector>
#include<bitset>
#include<unordered_map>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
// #include "boost/algorithm/string.hpp"
#define fio ios_base::sync_with_stdio(false)
#define mod 1000000007
#define mod1 mod
#define mod2 1000000007
#define li long long int
#define ll li
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define readi(x) scanf("%d",&x)
#define  reads(x)  scanf("%s", x)
#define readl(x) scanf("%I64d",&x)
#define rep(i,n) for(i=0;i<n;i++)
#define revp(i,n) for(i=(n-1);i>=0;i--)
#define myrep1(i,a,b) for(i=a;i<=b;i++)
#define myrep2(i,a,b) for(i=b;i>=a;i--)
#define pb push_back
#define mp make_pair
#define fi first
#define sec second
#define MAXN 1000000000000000005
#define MINN -1000000000000000000
#define INTMAX 1000001000
#define pii pair<ll,ll>
#define pdd pair<double,double>
#define pic pair<int,char>
#define N 4005
#define lgn 20
#define ddouble long double
#define minus minu
#define PI 3.1415926535
#define lgn 20

// #define INTMAX 200000000

// using namespace boost;
// #define si short int

using namespace std;
using namespace __gnu_pbds;
typedef priority_queue<ll, vector<ll > > max_pq;
typedef priority_queue<ll, vector<ll> , greater<ll>  > min_pq;
ll toint(const string &s) {stringstream ss; ss << s; ll x; ss >> x; return x;}
string tostring ( ll number ){stringstream ss; ss<< number; return ss.str();}

// using namespace __gnu_pbds;
// typedef priority_queue<pair<ll,char> , vector<pair<ll , char> > > max_pq;
// typedef priority_queue<pii , vector<pii > ,greater<pii > > min_pq;
typedef tree<pii , null_type, less<pii>, rb_tree_tag, tree_order_statistics_node_update> OST;
// typedef priority_queue<pair < ll , pair < pii , ll > >  , vector<pair < ll , pair < pii , ll > >  > ,greater<pair < ll , pair < pii , ll > >  > > min_pq;
#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
    template <typename Arg1>
    void __f(const char* name, Arg1&& arg1){
        cout << name << " : " << arg1 << std::endl;
        //use cerr if u want to display at the bottom
    }
    template <typename Arg1, typename... Args>
    void __f(const char* names, Arg1&& arg1, Args&&... args){
        const char* comma = strchr(names + 1, ','); cout.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
    }
#else
#define trace(...)
#endif
#define nn 2000005
ll l,r,t;
ll dp[35][ 2 ][ 2 ][ 2 ][ 2 ];
ll solve ( ll pos, ll tl1, ll tr1, ll tl2, ll tr2 ){
    if ( pos == -1 ){
        return 1;
    }
    if ( dp[pos][tl1][tr1][tl2][tr2] != -1 ){
        return dp[pos][tl1][tr1][tl2][tr2];
    }
    ll lbit = ((1LL<<pos)&l)?1:0;
    ll rbit = ((1LL<<pos)&r)?1:0;
    ll sl = ((tl1)?lbit:0);
    ll el = ((tr1)?rbit:1);
    ll sr = ((tl2)?lbit:0);
    ll er = ((tr2)?rbit:1);
    ll ret = 0;
    for ( ll i = sl; i <= el; i ++){
        for ( ll j = sr; j <= er; j ++){
            if ( (i==0 or j == 0)){
                ret += solve(pos-1,tl1&(sl==i),tl2&(el==i),tl2&(sr==j),tr2&(er==j));
            }
        }
    }
    return dp[pos][tl1][tr1][tl2][tr2] = ret;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    #endif
    ll t;
    cin >> t;
    while ( t-- ){
        cin >> l >> r;
        memset(dp,-1,sizeof dp);
        cout << solve(31,1,1,1,1) <<"\n";
    }
}


#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<bits/stdc++.h>
#include<stack>
#include<queue>
#include<list>
#include<vector>
#include<bitset>
#include<unordered_map>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
// #include "boost/algorithm/string.hpp"
#define fio ios_base::sync_with_stdio(false)
#define mod 1000000007
#define mod1 mod
#define mod2 1000000007
#define li long long int
#define ll li
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define readi(x) scanf("%d",&x)
#define  reads(x)  scanf("%s", x)
#define readl(x) scanf("%I64d",&x)
#define rep(i,n) for(i=0;i<n;i++)
#define revp(i,n) for(i=(n-1);i>=0;i--)
#define myrep1(i,a,b) for(i=a;i<=b;i++)
#define myrep2(i,a,b) for(i=b;i>=a;i--)
#define pb push_back
#define mp make_pair
#define fi first
#define sec second
#define MAXN 1000000000000000005
#define MINN -1000000000000000000
#define INTMAX 1000001000
#define pii pair<ll,ll>
#define pdd pair<double,double>
#define pic pair<int,char>
#define N 4005
#define lgn 20
#define ddouble long double
#define minus minu
#define PI 3.1415926535
#define lgn 20

// #define INTMAX 200000000

// using namespace boost;
// #define si short int

using namespace std;
using namespace __gnu_pbds;
typedef priority_queue<ll, vector<ll > > max_pq;
typedef priority_queue<ll, vector<ll> , greater<ll>  > min_pq;
ll toint(const string &s) {stringstream ss; ss << s; ll x; ss >> x; return x;}
string tostring ( ll number ){stringstream ss; ss<< number; return ss.str();}

// using namespace __gnu_pbds;
// typedef priority_queue<pair<ll,char> , vector<pair<ll , char> > > max_pq;
// typedef priority_queue<pii , vector<pii > ,greater<pii > > min_pq;
typedef tree<pii , null_type, less<pii>, rb_tree_tag, tree_order_statistics_node_update> OST;
// typedef priority_queue<pair < ll , pair < pii , ll > >  , vector<pair < ll , pair < pii , ll > >  > ,greater<pair < ll , pair < pii , ll > >  > > min_pq;
#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
    template <typename Arg1>
    void __f(const char* name, Arg1&& arg1){
        cout << name << " : " << arg1 << std::endl;
        //use cerr if u want to display at the bottom
    }
    template <typename Arg1, typename... Args>
    void __f(const char* names, Arg1&& arg1, Args&&... args){
        const char* comma = strchr(names + 1, ','); cout.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
    }
#else
#define trace(...)
#endif
#define nn 2000005
ll l,r,t;
ll dp[35][ 2 ][ 2 ][ 2 ][ 2 ];
ll solve ( ll pos, ll tl1, ll tr1, ll tl2, ll tr2 ){
    if ( pos == -1 ){
        return 1;
    }
    if ( dp[pos][tl1][tr1][tl2][tr2] != -1 ){
        return dp[pos][tl1][tr1][tl2][tr2];
    }
    ll lbit = ((1LL<<pos)&l)?1:0;
    ll rbit = ((1LL<<pos)&r)?1:0;
    ll sl = ((tl1)?lbit:0);
    ll el = ((tr1)?rbit:1);
    ll sr = ((tl2)?lbit:0);
    ll er = ((tr2)?rbit:1);
    ll ret = 0;
    for ( ll i = sl; i <= el; i ++){
        for ( ll j = sr; j <= er; j ++){
            if ( (i==0 or j == 0)){
                ret += solve(pos-1,tl1&(sl==i),tl2&(el==i),tr1&(sr==j),tr2&(er==j));
            }
        }
    }
    return dp[pos][tl1][tr1][tl2][tr2] = ret;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    #endif
    ll t;
    cin >> t;
    while ( t-- ){
        cin >> l >> r;
        memset(dp,-1,sizeof dp);
        cout << solve(31,1,1,1,1) <<"\n";
    }
}


#include<bits/stdc++.h>

#define pb push_back
#define x first
#define y second
using namespace std;
typedef long long INT;
typedef pair<int, int> pii;
typedef vector<int> VI;
int power(int a, int b, int m, int ans=1) {
	for(; b; b>>=1, a=1LL*a*a%m) if(b&1) ans=1LL*ans*a%m;
	return ans;
}

#define NN 30
INT dp[NN][2][2];

INT calc(int x, int y, int i, int j, int pos) {
	if(pos == -1) return 1;
	if(~dp[pos][i][j]) return dp[pos][i][j];
	INT &ans = dp[pos][i][j];
	ans = 0;
	int a = (x>>pos) & 1;
	int b = (y>>pos) & 1;
	int eda=1, edb=1;
	if(i==0 and a==0) eda=0;
	if(j==0 and b==0) edb=0;
	for(int q=0; q<=eda; q++) {
		for(int w=0; w<=edb; w++) if(q+w<2) {
			ans += calc(x, y, i|(q<a), j|(w<b), pos-1);
		}
	}
	return ans;
}

INT calc(int x, int y) {
	if(x < 0 or y < 0) return 0;
	memset(dp, -1, sizeof dp);
	return calc(x, y, 0, 0, 29);
}

int solve() {
	int l, r;
	cin >> l >> r;
	INT ans = calc(r, r) - calc(r, l-1) * 2 + calc(l-1, l-1);
	cout << ans << endl;
}

int main() {
#ifndef	ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif
	int T;
	cin >> T;
	while(T--) solve();
	return 0;
}


#include<bits/stdc++.h>
#define pb push_back
#define y second
#define x first

using namespace std;
typedef long long INT;
typedef pair<int, int> pii;
template<typename T, typename U> inline void smin(T &a, const U &b) {if(a>b) a=b;}
template<typename T, typename U> inline void smax(T &a, const U &b) {if(a<b) a=b;}

int main() {
#ifdef kong
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif

	int t; scanf("%d", &t);
	while(t--) {
		int a, b; scanf("%d %d", &a, &b);
		int ans=0;
		for(int i=a; i<=b; i++) {
			for(int j=a; j<i; j++) if(!(i&j)) ans++;
		}
		printf("%d\n", ans<<1);
	}
}


#include <bits/stdc++.h>

using namespace std;
typedef long long INT;

#define NN 100
int a[NN],b[NN];

INT calc(int A,int B){
	if(A>B) swap(A,B);
	int x=A,n=0,m=0;
	while(x>0){
		a[++n]=x%2;
		x/=2;
	}
	reverse(a+1,a+n+1);
	x=B;
	while(x>0){
		b[++m]=x%2;
		x/=2;
	}
	reverse(b+1,b+m+1);
	INT ans=0;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			if(!a[i]||!b[j]) continue;
			a[i]=0,b[j]=0;
			INT tot=1;
			for(int k=1;k<=m;k++){
				if(k<=m-n){
					if(k>j){
						tot*=2;
					}
				}
				else{
					if(k<=j&&k-m+n<=i){
						if(b[k]&&a[k-m+n]){
							tot=0;
							break;
						}
					}
					else if(k>j&&k-m+n>i) tot*=3;
					else{
						if(k<=j&&!b[k]) tot*=2;
					    else if(k-m+n<=i&&!a[k-m+n]) tot*=2;
					}
				}
			}
			ans+=tot;
			a[i]=1,b[j]=1;
		}
	}
	return ans;
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif

	int T;
	scanf("%d",&T);
	while(T--){
		int L,R;
		scanf("%d%d",&L,&R);
		R++;
		printf("%I64d\n",calc(R,R)-calc(L,R)-calc(R,L)+calc(L,L));
	}
    return 0;
}


#include<bits/stdc++.h>

#define x first
#define y second

using namespace std;

typedef long long INT;

int calc(int L, int x) {
	if(L < 0) return 0;
	if(L == 0) {
		if((L^x) == (L + x)) return 1;
		return 0;
	}

	int id = 31 - __builtin_clz(L);

	if((1<<id+1) == L + 1) id++;

	int cnt = 0;
	for(int j=0; j<id; j++) if(((x>>j) & 1) == 0) {
		cnt++;
	}

	int ans = (1<<cnt);
	if((x>>id)&1) return ans;
	return ans + calc(L - (1<<id), x);
}

INT solve(int L, int R) {
	if(L == R) {
		if((L^L) == L*2) return 1;
		return 0;
	}
	if(L % 2 == 1) {
		int res = 0;
		if((L^L) == L * 2) res = 1;
		res += 2ll * (calc(R, L) - calc(L, L));
		return res + solve(L+1, R);
	}
	if(R % 2 == 0) {
		INT res = 0;
		if((R^R) == R*2) res = 1;
		res += (INT) 2 * (calc(R-1, R) - calc(L-1, R));
		return res + solve(L, R - 1);
	}
	return solve(L / 2, R / 2) * 3;
}

int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
	freopen("out.out","w",stdout);
#endif
	int T;

	cin >> T;

	while(T--) {
		int L, R;
		cin >> L >> R;
		cout << solve(L, R) << endl;
	}

	return 0;
}