#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <cassert>

using namespace std;

typedef long long LL;
typedef pair<int,int> PII;
#define MP make_pair
#define FOR(v,p,k) for(int v=p;v<=k;++v)
#define FORD(v,p,k) for(int v=p;v>=k;--v)
#define REP(i,n) for(int i=0;i<(n);++i)
#define VAR(v,i) __typeof(i) v=(i)
#define FOREACH(i,c) for(__typeof(c.begin()) i=(c.begin());i!=(c).end();++i)
#define PB push_back
#define ST first
#define ND second
#define SIZE(x) (int)x.size()
#define ALL(c) c.begin(),c.end()
#define ZERO(x) memset(x,0,sizeof(x))

const int N = 2000;

int price[N], occ[N];

LL dp[N];

int n;

int main() {
	LL sum = 0;
	scanf("%d", &n);
	for (int i = 0; i < n; ++i) {
		scanf("%d %d", &occ[i], &price[i]);
		sum += price[i];
	}
	for (int i = 1; i <= n; ++i)
		dp[i] = sum;
	for (int i = 0; i < n; ++i) {
		for (int j = n; j >= 0; --j) {
			dp[min(n, j + occ[i] + 1)] =
					min(dp[min(n, j + occ[i] + 1)],
							dp[j] + price[i]);
		}
	}
	cout << dp[n] << "\n";
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <cassert>

using namespace std;

typedef long long LL;
typedef pair<int,int> PII;
#define MP make_pair
#define FOR(v,p,k) for(int v=p;v<=k;++v)
#define FORD(v,p,k) for(int v=p;v>=k;--v)
#define REP(i,n) for(int i=0;i<(n);++i)
#define VAR(v,i) __typeof(i) v=(i)
#define FOREACH(i,c) for(__typeof(c.begin()) i=(c.begin());i!=(c).end();++i)
#define PB push_back
#define ST first
#define ND second
#define SIZE(x) (int)x.size()
#define ALL(c) c.begin(),c.end()
#define ZERO(x) memset(x,0,sizeof(x))

const int N = 2000;

int price[N], occ[N];

LL dp[N + 1];

int n;

int main() {
	LL sum = 0;
	cin >> n;
	for (int i = 0; i < n; ++i) {
		cin >> occ[i] >> price[i];
		sum += price[i];
	}
	for (int i = 1; i <= n; ++i)
		dp[i] = sum;
	for (int i = 0; i < n; ++i) {
		for (int j = n; j >= 0; --j) {
			dp[min(n, j + occ[i] + 1)] =
					min(dp[min(n, j + occ[i] + 1)],
							dp[j] + price[i]);
		}
	}
	cout << dp[n] << "\n";
}


#include <iostream>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <algorithm>
using namespace std;

long long cost[2002];
int it[2002];
long long cpy[2002];

struct item{
	int c;
	int t;
	bool operator<(const item &sec) const{
		return (t != sec.t) ? t < sec.t : c < sec.c;
	}
};
item cst[2002];

int main(){
	int n;
	cost[0] = 1;
	cin >> n;
	for (int i=0; i<n; i++){
		int t, c;
		memcpy(cpy, cost, sizeof cost);
		cin >> t >> c;
		cst[i].c = c;
		cst[i].t = t;
		for (int i=0; i<n; i++){
			int tm = min(i + t, n);
			if (cost[i]){
				if (!cpy[tm] || cpy[tm] > cost[i] + c || (cpy[tm] == cost[i] + c && it[tm] < it[i] + 1)){
					cpy[tm] = cost[i] + c;
					it[tm] = it[i] + 1;
				}
			}
		}
		memcpy(cost, cpy, sizeof cost);
	}
	sort(cst, cst+n);
	long long ret = 1ll<<60;
	for (int i=0; i<=n; i++){
		long long now = cost[i];
		if (!now)
			continue;
		for (int j=0; j < n - it[i] - i; j++){
			now += cst[j].c;
			if (cst[j].t){
				now = 1ll<<60;
				break;
			}
		}

		ret = min(ret, now);
	}
	cout << ret - 1 << endl;
	return 0;
}


#include <iostream>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <algorithm>
using namespace std;

#define mp(a,b) make_pair(a, b)
map< pair<int ,int>, long long> F;
map< pair<int ,int>, long long> S;

int main(){
	int n;
	F[mp(0,0)] = 0;
	cin >> n;
	for (int i=0; i<n; i++){
		int t, c;
		cin >> t >> c;
		S = F;
		for (map< pair<int ,int>, long long> :: iterator i = F.begin(); i != F.end(); i++){
			pair<int ,int> tec = i->first;
			long long val = i->second + c;
			tec.first = min(n, tec.first+t);
			tec.second++;
			if (S.find(tec) == S.end() || S[tec] > val){
				S[tec] = val;
			}
		}
		F = S;
	}
	long long ret = 1ll<<60;
	for (map< pair<int ,int>, long long> :: iterator i = F.begin(); i != F.end(); i++){
		pair<int ,int> tec = i->first;
		long long val = i->second;
		if (tec.first + tec.second >= n)
			ret = min(ret, val);
	}
	cout << ret << endl;
	return 0;
}


#include <iostream>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <algorithm>
using namespace std;

long long cost[2002];
int it[2002];
long long cpy[2002];

struct item{
	int c;
	int t;
	bool operator<(const item &sec) const{
		return (t != sec.t) ? t < sec.t : c < sec.c;
	}
};
item cst[2002];

int main(){
	int n;
	cost[0] = 1;
	cin >> n;
	for (int i=0; i<n; i++){
		int t, c;
		memcpy(cpy, cost, sizeof cost);
		cin >> t >> c;
		cst[i].c = c;
		cst[i].t = t;
		for (int i=0; i<n; i++){
			int tm = min(i + t, n);
			if (cost[i]){
				if (!cpy[tm] || cpy[tm] > cost[i] + c || (cpy[tm] == cost[i] + c && it[tm] < it[i] + 1)){
					cpy[tm] = cost[i] + c;
					it[tm] = it[i] + 1;
				}
			}
		}
		memcpy(cost, cpy, sizeof cost);
	}
	sort(cst, cst+n);
	long long ret = 1ll<<60;
	for (int i=0; i<=n; i++){
		long long now = cost[i];
		if (!now)
			continue;
		for (int j=0; j < n - it[i] - i; j++){
			now += cst[j].c;
		}

		ret = min(ret, now);
	}
	cout << ret - 1 << endl;
	return 0;
}


#include <iostream>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <algorithm>
using namespace std;

long long cost[2002];
long long cpy[2002];

struct item{
	int c;
	int t;
	bool operator<(const item &sec) const{
		return (t != sec.t) ? t < sec.t : c < sec.c;
	}
};
item cst[2002];

int main(){
	int n;
	cost[0] = 1;
	cin >> n;
	for (int i=0; i<n; i++){
		int t, c;
		memcpy(cpy, cost, sizeof cost);
		cin >> t >> c;
		cst[i].c = c;
		cst[i].t = t;
		for (int j=0; j<n; j++){
			int tm = min(j + t + 1, n);
			if (cost[j]){
				if (!cpy[tm] || cpy[tm] > cost[j] + c){
					cpy[tm] = cost[j] + c;
				}
			}
		}
		memcpy(cost, cpy, sizeof cost);
	}
	sort(cst, cst+n);
	cout << cost[n] - 1 << endl;
	return 0;
}


#include <iostream>
#include <iomanip>
#include <set>
#include <map>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
#include <cmath>

using namespace std;

int main()
{
    long long LLMAX = 1LL << 60;
    vector<vector<long long> > dp(2005,vector<long long>(2005,LLMAX));

    int n;
    cin >> n;
    for(int i = 0 ; i < n ; i++)
    {
        int t,c;
        cin >> t >> c;
        int index;
        for(int j = 0 ; j <= n ; j++)
        {
            index = min(j+t+1,n);
            dp[i+1][j] = min(dp[i+1][j],dp[i][j]);
            dp[i+1][index] = min(dp[i+1][index],dp[i][j]+c);
        }
        index = min(t+1,n);
        dp[i+1][index] = min(dp[i+1][index],(long long)c);
    }
    cout << dp[n][n] << endl;
}


#include <stdio.h>
#include <math.h>
#include <string.h>
#include <iostream>
#include <fstream>
#include <algorithm>
#include <string>
#include <vector>
#include <set>
#include <map>
#pragma comment(linker, "/STACK:16777216")
using namespace std;

typedef vector<int> VI;
typedef pair<int,int> PII;
typedef __int64 LL;

#define bit(n) (1<<(n))
#define inf 1000000000
#define eps 1e-9
#define PI 3.1415926535897932385
#define pb push_back
#define sz size()
#define mp make_pair
#define cl clear()
#define all(a) a.begin(),a.end()
#define fill(ar,val) memset(ar,val,sizeof ar)
inline void MIN(LL &a,LL b){if(a>(b)) a=(b);}
#define MAX(a,b) if(a<(b)) a=(b)
#define sqr(x) ((x)*(x))
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))

#define N 2020

LL f[N][N];

int main()
{
	//freopen("in.txt","r",stdin);
	int n;
	while(scanf("%d",&n)>0)
	{
		int t,c;
		int i,j,k;
		for(j=0;j<=n;j++)
			for(k=0;k<=n;k++) f[j][k]=LL(inf)*inf;
		f[0][0]=0;
		for(i=0;i<n;i++)
		{
			scanf("%d%d",&t,&c);
			for(k=i;k>=0;k--)
				for(j=n-k-1;j>=0;j--)
					MIN(f[min(j+t,n)][k+1],f[j][k]+c);
		}
		LL ans=LL(inf)*inf;
		for(j=0;j<=n;j++)
			for(k=n-j;k<=n;k++)
				MIN(ans,f[j][k]);
		printf("%I64d\n",ans);
	}
	return 0;
}


#include <stdio.h>
#include <math.h>
#include <string.h>
#include <iostream>
#include <fstream>
#include <algorithm>
#include <string>
#include <vector>
#include <set>
#include <map>
#pragma comment(linker, "/STACK:16777216")
using namespace std;

typedef vector<int> VI;
typedef pair<int,int> PII;
typedef __int64 LL;

#define bit(n) (1<<(n))
#define inf 1000000000
#define eps 1e-9
#define PI 3.1415926535897932385
#define pb push_back
#define sz size()
#define mp make_pair
#define cl clear()
#define all(a) a.begin(),a.end()
#define fill(ar,val) memset(ar,val,sizeof ar)
inline void MIN(LL &a,LL b){if(a>(b)) a=(b);}
#define MAX(a,b) if(a<(b)) a=(b)
#define sqr(x) ((x)*(x))
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))

#define N 2020

LL f[N];

int main()
{
	//freopen("in.txt","r",stdin);
	int n;
	while(scanf("%d",&n)>0)
	{
		int t,c;
		int i,j,k;
		for(j=0;j<=n;j++) f[j]=LL(inf)*inf;
		f[0]=0;
		for(i=0;i<n;i++)
		{
			scanf("%d%d",&t,&c);
			for(k=n;k>=0;k--)
				MIN(f[min(k+t+1,n)],f[k]+c);
		}
		printf("%I64d\n",f[n]);
	}
	return 0;
}


#define _CRT_SECURE_NO_DEPRECATE

#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <iostream>
#include <sstream>
#include <ctime>

using namespace std;

template<class T> int size(const T& a) {
	return (int)a.size();
}
template<class T> T sqr(const T& a) {
	return a * a;
}

long long dm[4001];
int main() {
#ifdef pperm
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	memset(dm, 63, sizeof(dm));
	int n;
	scanf("%d", &n);
	dm[0] = 0;
	long long sum = 0;
	for (int i = 0; i < n; i++) {
		int t, c;
		scanf("%d %d", &t, &c);
		sum += c;
		t++;
		for (int j = 4000; j >= t; j--) {
			if (dm[j] > dm[j - t] + c) {
				dm[j] = dm[j - t] + c;
			}
		}
	}
	long long res = sum;
	for (int i = n; i <= 4000; i++) {
		res = min(res, dm[i]);
	}
	cout << res;
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <stack>
#include <string>
#include <algorithm>
#include <map>
#include <fstream>
#include <queue>
#include <cmath>
using namespace std;
int n;
typedef struct{
	long long t;
	long long c;
}ITEM;
ITEM allItems[2001];
long long dp[2001][2001];
long long solve(int now, int sum)
{
	if(now <= 0 && sum > 0) return 1000000000000000ll;
	if(sum <= 0) return 0;
	if(dp[now][sum] != -1) return dp[now][sum];

	long long res = 1000000000000000ll;
	res = min(res,  allItems[now].c + solve(now - 1, sum - allItems[now].t));
	res = min(res, solve(now - 1, sum));
	return dp[now][sum] = res;
}
int main()
{
	//freopen("..\\case.in","r",stdin);
	scanf("%d", &n);
	for(int i = 1;i <= n;i++)
	{
		scanf("%lld %lld", &allItems[i].t, &allItems[i].c);
		allItems[i].t += 1;
	}

	memset(dp, -1, sizeof(dp));
	//for(int i = 0;i <= n;i++)
	//	dp[1][i] = 1000000000000000ll;;
	//dp[1][allItems[1].t] = allItems[1].c;
	printf("%d\n", solve(n, n));
	//system("Pause");
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <stack>
#include <string>
#include <algorithm>
#include <map>
#include <fstream>
#include <queue>
#include <cmath>
using namespace std;
int n;
typedef struct{
	long long t;
	long long c;
}ITEM;
ITEM allItems[2001];
long long dp[2001][2001];
long long solve(int now, int sum)
{
	if(now <= 0 && sum > 0) return 1000000000000000ll;
	if(sum <= 0) return 0;
	if(dp[now][sum] != -1) return dp[now][sum];

	long long res = 1000000000000000ll;
	res = min(res,  allItems[now].c + solve(now - 1, sum - allItems[now].t));
	res = min(res, solve(now - 1, sum));
	return dp[now][sum] = res;
}
int main()
{
	//freopen("..\\case.in","r",stdin);
	scanf("%d", &n);
	for(int i = 1;i <= n;i++)
	{
		scanf("%lld %lld", &allItems[i].t, &allItems[i].c);
		allItems[i].t += 1;
	}

	memset(dp, -1, sizeof(dp));
	//for(int i = 0;i <= n;i++)
	//	dp[1][i] = 1000000000000000ll;;
	//dp[1][allItems[1].t] = allItems[1].c;
	printf("%lld\n", solve(n, n));
	//system("Pause");
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <stack>
#include <string>
#include <algorithm>
#include <map>
#include <fstream>
#include <queue>
#include <cmath>
using namespace std;
int n;
typedef struct{
	long long t;
	long long c;
}ITEM;
ITEM allItems[2001];
long long dp[2001][2001];
long long solve(int now, int sum)
{
	if(now <= 0 && sum > 0) return 1000000000000000ll;
	if(sum <= 0) return 0;
	if(dp[now][sum] != -1) return dp[now][sum];

	long long res = 1000000000000000ll;
	for(int i = 1;i <= now;i++)
	{
		if(allItems[i].t <= sum)
			res = min(res, allItems[i].c + solve(i - 1, sum - allItems[i].t));
	}
	return dp[now][sum] = res;
}
int main()
{
	//freopen("..\\case.in","r",stdin);
	scanf("%d", &n);
	for(int i = 1;i <= n;i++)
	{
		scanf("%lld %lld", &allItems[i].t, &allItems[i].c);
		allItems[i].t += 1;
	}

	memset(dp, -1, sizeof(dp));
	for(int i = 0;i <= n;i++)
		dp[1][i] = 1000000000000000ll;;
	dp[1][allItems[1].t] = allItems[1].c;
	printf("%d\n", solve(n, n));
	//system("Pause");
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <stack>
#include <string>
#include <algorithm>
#include <map>
#include <fstream>
#include <queue>
#include <cmath>
using namespace std;
int n;
typedef struct{
	long long t;
	long long c;
}ITEM;
ITEM allItems[2001];
long long dp[2001][2001];
long long solve(int now, int sum)
{
	if(now <= 0 && sum > 0) return 1000000000000000ll;
	if(sum == 0) return 0;
	if(dp[now][sum] != -1) return dp[now][sum];

	long long res = 1000000000000000ll;
	for(int i = 1;i <= now;i++)
	{
		if(allItems[i].t <= sum)
			res = min(res, allItems[i].c + solve(i - 1, sum - allItems[i].t));
	}
	return dp[now][sum] = res;
}
int main()
{
	//freopen("..\\case.in","r",stdin);
	scanf("%d", &n);
	for(int i = 1;i <= n;i++)
	{
		scanf("%lld %lld", &allItems[i].t, &allItems[i].c);
		allItems[i].t += 1;
	}

	memset(dp, -1, sizeof(dp));
	for(int i = 0;i <= n;i++)
		dp[1][i] = 1000000000000000ll;;
	dp[1][allItems[1].t] = allItems[1].c;
	printf("%d\n", solve(n, n));
	//system("Pause");
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <stack>
#include <string>
#include <algorithm>
#include <map>
#include <fstream>
#include <queue>
#include <cmath>
using namespace std;
int n;
typedef struct{
	long long t;
	long long c;
}ITEM;
ITEM allItems[2001];
long long dp[2001][2001];
long long solve(int now, int sum)
{
	if(now <= 0 && sum > 0) return 1000000000000000ll;
	if(sum <= 0) return 0;
	if(dp[now][sum] != -1) return dp[now][sum];

	long long res = 1000000000000000ll;
	res = min(res,  allItems[now].c + solve(now - 1, sum - allItems[now].t));
	res = min(res, solve(now - 1, sum));
	return dp[now][sum] = res;
}
int main()
{
	//freopen("..\\case.in","r",stdin);
	scanf("%d", &n);
	for(int i = 1;i <= n;i++)
	{
		scanf("%lld %lld", &allItems[i].t, &allItems[i].c);
		allItems[i].t += 1;
	}

	memset(dp, -1, sizeof(dp));
	for(int i = 0;i <= n;i++)
		dp[1][i] = 1000000000000000ll;;
	dp[1][allItems[1].t] = allItems[1].c;
	printf("%d\n", solve(n, n));
	//system("Pause");
	return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int MAXN = 4500 + 1;
const int DELTA = 2050;
const long long INF = 1012345678901234567LL;

int n;
long long dp[2][MAXN];

int main() {
	scanf("%d", &n);
	for (int i = 0; i <= n; i++) dp[0][i + DELTA] = INF;
	dp[0][DELTA] = 0;
	int t1 = 0, t2;
	for (int o = 0, low = 0, high = n; o < n; o++, low--, high--) {
		int x, y;
		scanf("%d%d", &x, &y);
		t2 = t1 ^ 1;
		for (int i = low; i <= high; i++) dp[t2][i - 1 + DELTA] = dp[t1][i + DELTA];
		for (int i = low; i <= high; i++) dp[t2][min(i + x, high - 1) + DELTA] = min(dp[t2][min(i + x, high - 1) + DELTA], dp[t1][i + DELTA] + y);
		t1 = t2;
	}
	printf("%lld\n", dp[t1][DELTA]);
	return 0;
}


#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
#include <sstream>
#include <iostream>
using namespace std;
typedef long long ll;

#define REP(i,n) for (int i=0; i<(int)(n); ++i)
#define FOR(i,k,n) for (int i=(k); i<(int)(n); ++i)
#define FOREQ(i,k,n) for (int i=(k); i<=(int)(n); ++i)
#define DEC(i,k) for (int i=(k); i>=0; --i)

#define SZ(v) (int)((v).size())
#define MEMSET(v,h) memset((v),(h),sizeof(v))
#define FIND(m,w) ((m).find(w)!=(m).end())

ll D[2002][2002];
ll t[2002],c[2002];

int main()
{
    int N; cin>>N;
    FOREQ(j,1,N) cin>>t[j]>>c[j];

    FOREQ(j,0,N) FOREQ(k,0,N) D[j][k]=1LL<<56;
    REP(j,N) D[j][0]=0;
    FOREQ(j,1,N) {
        FOREQ(k,0,N) {
            D[j][k]=D[j-1][k];
            int dk=k-(t[j]+1);
            if (dk<0) dk=0;
            D[j][k]=min(D[j][k], c[j]+D[j-1][dk]);
            //cout<<j<<","<<k<<" "<<D[j][k]<<endl;
        }
    }
    printf("%lld\n",D[N][N]);
}


#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
#include <sstream>
#include <iostream>
using namespace std;
typedef long long ll;

#define REP(i,n) for (int i=0; i<(int)(n); ++i)
#define FOR(i,k,n) for (int i=(k); i<(int)(n); ++i)
#define FOREQ(i,k,n) for (int i=(k); i<=(int)(n); ++i)
#define DEC(i,k) for (int i=(k); i>=0; --i)

#define SZ(v) (int)((v).size())
#define MEMSET(v,h) memset((v),(h),sizeof(v))
#define FIND(m,w) ((m).find(w)!=(m).end())

ll D[2002][2002];
ll t[2002],c[2002];

int main()
{
    int N; cin>>N;
    FOREQ(j,1,N) cin>>t[j]>>c[j];

    FOREQ(j,0,N) FOREQ(k,0,N) D[j][k]=1LL<<56;
    REP(j,N) D[j][0]=0;
    FOREQ(j,1,N) {
        FOREQ(k,0,N) {
            D[j][k]=D[j-1][k];
            if (k-(t[j]+1)>=0) D[j][k]=min(D[j][k], c[j]+D[j-1][k-(t[j]+1)]);
        }
    }
    printf("%lld\n",D[N][N]);
}


#include <iostream>
#include <cmath>
#include <cstdio>
#include <vector>
#include <set>
#include <algorithm>
#include <string>
#include <cstring>
#include <fstream>
#include <cstdlib>

using namespace std;

#ifndef ONLINE_JUDGE

fstream fin("input.txt", fstream::in);
fstream fout("output.txt", fstream::out);
FILE *file_in = fopen("input.txt", "rt");
FILE *file_out = fopen("output.txt", "wt");

#else

#define fin cin
#define fout cout
#define file_in stdin
#define file_out stdout

#endif

int main() {
    int n;
    fin >> n;
    vector< int > t(n);
    vector< long long > c(n);
    for (int i = 0; i < n; ++i) {
        fin >> t[i] >> c[i];
    }
    vector< vector< long long > > minCost(n + 1, vector< long long >(2 * n + 1, 1e15));
    minCost[0][n] = 0;
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j <= 2 * n; ++j) {
            minCost[i + 1][min(j + t[i], 2 * n)] = min(minCost[i + 1][min(j + t[i], 2 * n)], minCost[i][j] + c[i]);
            if (j > 0) {
                minCost[i + 1][j - 1] = min(minCost[i + 1][j - 1], minCost[i][j]);
            }
        }
    }
    long long ans = 1e15;
    for (int i = n; i <= 2 * n; ++i) {
        ans = min(ans, minCost[n][i]);
    }
    fout << ans << '\n';

    return 0;
}


//#pragma GCC optimize(2)
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ls o<<1
#define rs o<<1|1
#define pii pair<int,int>
#define fi first
#define se second
const double pi=acos(-1.0);
const double eps=1e-10;
const ll mod=1e9+7;
const int INF=0x3f3f3f3f;
ll read(){
	ll x=0,f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}
int t,l,r;
ll g(int a,int b){  //[a,b)
	int cnt=0,res=0;
	for(int i=1;i<=b;i<<=1){
		if(b&i){
			b^=i;
			if(!(a&b)){
				res+=(1<<cnt);
			}
		}
		if(!(a&i)){
			cnt++;
		}
	}
	return res;
}
ll f(int l,int r){
	//printf("%d %d\n",l,r);
	if(l>=r)return 0;
	if(l==0){
		ll tmp=f(1,r)+2*r-1;
		//printf("*%lld\n",tmp);
		return tmp;
	}
	if(l%2!=0){
		ll tmp=f(l+1,r)+2*(g(l,r)-g(l,l));
		//printf("**%lld\n",tmp);
		return tmp;
	}
	if(r%2!=0){
		ll tmp=f(l,r-1)+2*(g(r-1,r)-g(r-1,l));
		//printf("***%lld\n",tmp);
		return tmp;
	}
	ll tmp=3*f(l/2,r/2);
	//printf("****%lld\n",tmp);
	return tmp;
}
int main(void){
	//freopen("in.txt","r",stdin);
	scanf("%d",&t);
	while(t--){
		scanf("%d%d",&l,&r);
		printf("%lld\n",f(l,r+1));
	}
	return 0;
}


//#pragma GCC optimize(2)
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ls o<<1
#define rs o<<1|1
#define pii pair<int,int>
#define fi first
#define se second
const double pi=acos(-1.0);
const double eps=1e-10;
const ll mod=1e9+7;
const int INF=0x3f3f3f3f;
ll read(){
	ll x=0,f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}
int t,l,r;
ll g(int a,int b){  //[a,b)
	int cnt=0,res=0;
	for(int i=1;i<=b;i<<=1){
		if(b&i){
			b^=i;
			if(!(a&b)){
				res+=(1<<cnt);
			}
		}
		if(!(a&i)){
			cnt++;
		}
	}
	return res;
}
ll f(int l,int r){
	//printf("%d %d\n",l,r);
	if(l>=r)return 0;
	if(l==0){
		ll tmp=f(1,r)+2*(r-1);
		//printf("*%lld\n",tmp);
		return tmp;
	}
	if(l%2!=0){
		ll tmp=f(l+1,r)+2*(g(l,r)-g(l,l));
		//printf("**%lld\n",tmp);
		return tmp;
	}
	if(r%2!=0){
		ll tmp=f(l,r-1)+2*(g(r-1,r)-g(r-1,l));
		//printf("***%lld\n",tmp);
		return tmp;
	}
	ll tmp=3*f(l/2,r/2);
	//printf("****%lld\n",tmp);
	return tmp;
}
int main(void){
	//freopen("in.txt","r",stdin);
	scanf("%d",&t);
	while(t--){
		scanf("%d%d",&l,&r);
		printf("%lld\n",f(l,r+1));
	}
	return 0;
}


#include <cstdio>
#include <cctype>
#include <cstring>
#include <algorithm>
using namespace std;
#define File(s) freopen(s".in", "r", stdin), freopen(s".out", "w", stdout)
typedef long long ll;
template<typename T> inline void in(T &x){ //Read Positive Integer.
	char ch; x = 0;
	// int f = 1;
	while(isspace(ch = getchar()));
	// if(ch == '-') ch = getchar(), f = -1;
	do x = x * 10 + ch - '0'; while(isdigit(ch = getchar()));
	// x *= f;
}

ll f[60][2][2];
int a[60], b[60];

ll dfs(int p, bool x, bool y){
	if(p == 32)
		return 1 + (int)(a[32] == 1 || x) + (int)(b[32] == 1 || y);
	ll &res = f[p][x][y];
	if(f[p][x][y] != -1) return f[p][x][y];
	res = 0;
	for(int i=0; i<2; i++)
		for(int j=0; j<2; j++){
			if(i == 1 && j == 1) continue;
			if(a[p] == 0 && !x && i == 1) continue;
			if(b[p] == 0 && !y && j == 1) continue;
			res += dfs(p+1, x || i < a[p], y || j < b[p]);
		}
	return res;
}

ll dp(ll x, ll y){
	// printf("dp %d %d\n", x, y);
	memset(a, 0, sizeof(a)); memset(b, 0, sizeof(b));
	for(int i=32; i>=0; i--){
		a[i] = x & 1;
		b[i] = y & 1;
		x >>= 1; y >>= 1;
	}
	memset(f, -1, sizeof(f));
	// printf("Return %lld\n", dfs(0, 0, 0));
	return dfs(0, 0, 0);
}

int main(){
	// File("f");
	int T;
	in(T);
	while(T--){
		ll l, r;
		in(l); in(r);
		if(l == 0) printf("%lld\n", dp(r, r));
		else printf("%lld\n", dp(r, r) - dp(l-1, r) * 2 + dp(l-1, l-1));
	}
	return 0;
}


#include <cstdio>
#include <cctype>
#include <cstring>
#include <algorithm>
using namespace std;
#define File(s) freopen(s".in", "r", stdin), freopen(s".out", "w", stdout)
typedef long long ll;
template<typename T> inline void in(T &x){ //Read Positive Integer.
	char ch; x = 0;
	// int f = 1;
	while(isspace(ch = getchar()));
	// if(ch == '-') ch = getchar(), f = -1;
	do x = x * 10 + ch - '0'; while(isdigit(ch = getchar()));
	// x *= f;
}

ll f[60][2][2];
int a[60], b[60];

ll dfs(int p, bool x, bool y){
	if(p == 32)
		return 1 + (int)(a[32] == 1 || x) + (int)(b[32] == 1 || y);
	ll &res = f[p][x][y];
	if(f[p][x][y] != -1) return f[p][x][y];
	res = 0;
	for(int i=0; i<2; i++)
		for(int j=0; j<2; j++){
			if(i == 1 && j == 1) continue;
			if(a[p] == 0 && !x && i == 1) continue;
			if(b[p] == 0 && !y && j == 1) continue;
			res += dfs(p+1, x || i < a[p], y || j < b[p]);
		}
	return res;
}

ll dp(ll x, ll y){
	// printf("dp %d %d\n", x, y);
	memset(a, 0, sizeof(a)); memset(b, 0, sizeof(b));
	for(int i=32; i>=0; i--){
		a[i] = x & 1;
		b[i] = y & 1;
		x >>= 1; y >>= 1;
	}
	memset(f, -1, sizeof(f));
	// printf("Return %lld\n", dfs(0, 0, 0));
	return dfs(0, 0, 0);
}

int main(){
	// File("f");
	int T;
	in(T);
	while(T--){
		ll l, r;
		in(l); in(r);
		if(l == 0) printf("%lld\n", dp(r, r));
		printf("%lld\n", dp(r, r) - dp(l-1, r) * 2 + dp(l-1, l-1));
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
using ll=long long;

const int LG=32;
ll dp[LG][2][2];

ll calc(int idx, int t1, int t2, int l, int r) {
    if (idx==-1)
        return 1;
    if (dp[idx][t1][t2]!=-1)
        return dp[idx][t1][t2];

    int k1 = (t1) ? ((l>>idx)&1) : 1;
    int k2 = (t2) ? ((r>>idx)&1) : 1;

    ll res=0;
    for (int x=0; x<=k1; ++x)
        for (int y=0; y<=k2; ++y)
            if ((x&y)==0)
                res+=calc(idx-1, (t1&(x==k1)), (t2&(y==k2)), l, r);

    return (dp[idx][t1][t2]=res);
}

ll solve(int l, int r) {
    if (l<0 || r<0) return 0;
    for (int i=0; i<LG; ++i)
        for (int x=0; x<2; ++x)
            for (int y=0; y<2; ++y)
                dp[i][x][y]=-1;
    return calc(LG-1, 1, 1, l, r);
}

int main() {
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    int t; cin>>t;
    while (t--) {
        int l,r; cin>>l>>r;
        cout<<solve(r,r)-2LL*solve(l-1,r)+solve(l-1,l-1)<<"\n";
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
using ll=long long;
const int LG=31;

ll dp[LG][2][2];

ll calc(int idx, int t1, int t2, int l, int r) {
    if (idx==-1) return 1;
    if (dp[idx][t1][t2]!=-1) return dp[idx][t1][t2];
    ll res=0;
    int k1 = (t1) ? ((l>>idx)&1) : 1;
    int k2 = (t2) ? ((r>>idx)&1) : 1;
    for (int x=0; x<=k1; ++x) {
        for (int y=0; y<=k2; ++y) {
            if ((x&y)==0) {
                res+=calc(idx-1, (t1&(x==k1)), (t2&(y==k2)), l, r);
            }
        }
    }
    return (dp[idx][t1][t2]=res);
}

ll solve(int l, int r) {
    if (l==-1 || r==-1) return 0;
    for (int i=0; i<LG; ++i) {
        for (int x=0; x<2; ++x) {
            for (int y=0; y<2; ++y) {
                dp[i][x][y]=-1;
            }
        }
    }
    return calc(LG-1, 1, 1, l, r);
}

int main() {
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    int t; cin>>t;
    while (t--) {
        int l, r; cin>>l>>r;
        cout<<solve(r,r)-2*solve(l-1,r)+solve(l-1,l-1)<<"\n";
    }
    return 0;
}


#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma,tune=native")
#include <bits/stdc++.h>
using namespace std;
using ll=long long;

const int LG=32;
ll dp[LG][2][2];

ll calc(int idx, int t1, int t2, int l, int r) {
    if (idx==-1)
        return 1;
    if (dp[idx][t1][t2]!=-1)
        return dp[idx][t1][t2];

    int k1 = (t1) ? ((l>>idx)&1) : 1;
    int k2 = (t2) ? ((r>>idx)&1) : 1;

    ll res=0;
    for (int x=0; x<=k1; ++x)
        for (int y=0; y<=k2; ++y)
            if ((x&y)==0)
                res+=calc(idx-1, (t1&(x==k1)), (t2&(y==k2)), l, r);

    return (dp[idx][t1][t2]=res);
}

ll solve(int l, int r) {
    if (l==-1 || r==-1) return 0;
    for (int i=0; i<LG; ++i)
        for (int x=0; x<2; ++x)
            for (int y=0; y<2; ++y)
                dp[i][x][y]=-1;
    return calc(LG-1, 1, 1, l, r);
}

int main() {
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    int t; cin>>t;
    while (t--) {
        int l,r; cin>>l>>r;
        cout<<solve(r,r) - solve(l-1,r)<<1LL + solve(l-1,l-1)<<"\n";
    }
    return 0;
}


#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma,tune=native")
#include <bits/stdc++.h>
using namespace std;
using ll=long long;

const int LG=32;
ll dp[LG][2][2];

ll calc(int idx, int t1, int t2, int l, int r) {
    if (idx==-1)
        return 1;
    if (dp[idx][t1][t2]!=-1)
        return dp[idx][t1][t2];

    int k1 = (t1) ? ((l>>idx)&1) : 1;
    int k2 = (t2) ? ((r>>idx)&1) : 1;

    ll res=0;
    for (int x=0; x<=k1; ++x)
        for (int y=0; y<=k2; ++y)
            if ((x&y)==0)
                res+=calc(idx-1, (t1&(x==k1)), (t2&(y==k2)), l, r);

    return (dp[idx][t1][t2]=res);
}

ll solve(int l, int r) {
    if (l==-1 || r==-1) return 0;
    for (int i=0; i<LG; ++i)
        for (int x=0; x<2; ++x)
            for (int y=0; y<2; ++y)
                dp[i][x][y]=-1;
    return calc(LG-1, 1, 1, l, r);
}

int main() {
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    int t; cin>>t;
    while (t--) {
        int l,r; cin>>l>>r;
        cout<<solve(r,r) - (solve(l-1,r)<<1LL) + solve(l-1,l-1)<<"\n";
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn=3e5+5;
const int mod=1e9+7;
#define pb push_back
#define fi first
#define se second
#define all(x) (x).begin(),(x).end()
#define rep(i,a,n) for (int i=a;i<=n;i++)
#define per(i,a,n) for (int i=n;i>=a;i--)
typedef long long ll;
typedef double db;
typedef vector<int> vi;
typedef pair<int,int> pii;
ll qpow(ll a,ll b){ll ans=1;a%=mod;assert(b>=0);for(;b;b>>=1){if(b&1)ans=ans*a%mod;a=a*a%mod;}return ans;}
ll gcd(ll a,ll b){return b>0?gcd(b,a%b):a;}
int n,m,T;
int a[maxn];
int X,Y;
ll dp[40][2][2][2];
ll dfs(int len,bool up1,bool up2,int lst){
	if(len==-1)
		return 1;
	if(dp[len][up1][up2][lst])
		return dp[len][up1][up2][lst];
	int top1,top2;
	if(up1) top1=(X>>len)&1;
	else top1=1;
	if(up2) top2=(Y>>len)&1;
	else top2=1;
	ll res=0;
	for(int i=0;i<=top1;i++){
		for(int j=0;j<=top2;j++){
			if(i+j>=2)continue;
			res+=dfs(len-1,up1&&i==top1,up2&&j==top2,i^j);
			if(res>1000){
				int xxxxxx=0;
			}
		}
	}
	return dp[len][up1][up2][lst]=res;
}
ll cal(int A,int B){
	memset(dp,0,sizeof dp);
	X=A,Y=B;
	return dfs(31,1,1,0);
}
int main(){
	cin>>T;
	int l,r;
	while(T--){
		cin>>l>>r;
		if(l==0){
			cout<<cal(r,r)<<endl;
			continue;
		}
		ll x11=cal(r,r);
		ll x10=cal(r,l-1);
		ll x01=cal(l-1,r);
		ll x00=cal(l-1,l-1);
		cout<<x11-x10-(x01-x00)<<endl;
	}
//	cin>>n>>m;

	return 0;
}
/*

*/


#include<bits/stdc++.h>
using namespace std;
const int maxn=3e5+5;
const int mod=1e9+7;
#define pb push_back
#define fi first
#define se second
#define all(x) (x).begin(),(x).end()
#define rep(i,a,n) for (int i=a;i<=n;i++)
#define per(i,a,n) for (int i=n;i>=a;i--)
typedef long long ll;
typedef double db;
typedef vector<int> vi;
typedef pair<int,int> pii;
ll qpow(ll a,ll b){ll ans=1;a%=mod;assert(b>=0);for(;b;b>>=1){if(b&1)ans=ans*a%mod;a=a*a%mod;}return ans;}
ll gcd(ll a,ll b){return b>0?gcd(b,a%b):a;}
int n,m,T;
int a[maxn];
int X,Y;
ll dp[40][2][2][2];
ll dfs(int len,bool up1,bool up2,int lst){
	if(len==-1)
		return 1;
	if(dp[len][up1][up2][lst])
		return dp[len][up1][up2][lst];
	int top1,top2;
	if(up1) top1=(X>>len)&1;
	else top1=1;
	if(up2) top2=(Y>>len)&1;
	else top2=1;
	ll res=0;
	for(int i=0;i<=top1;i++){
		for(int j=0;j<=top2;j++){
			if(i+j>=2)continue;
			res+=dfs(len-1,up1&&i==top1,up2&&j==top2,i^j);
			if(res>1000){
				int xxxxxx=0;
			}
		}
	}
	return dp[len][up1][up2][lst]=res;
}
ll cal(int A,int B){
	memset(dp,0,sizeof dp);
	X=A,Y=B;
	return dfs(31,1,1,0);
}
int main(){
	cin>>T;
	int l,r;
	while(T--){
		cin>>l>>r;
		ll x11=cal(r,r);
		ll x10=cal(r,l-1);
		ll x01=cal(l-1,r);
		ll x00=cal(l-1,l-1);
		cout<<x11-x10-(x01-x00)<<endl;
	}
//	cin>>n>>m;

	return 0;
}
/*

*/


#include <bits/stdc++.h>
using namespace std;
#define fst first
#define snd second
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
#define pb push_back
#define for_tests(t, tt) int t; scanf("%d", &t); for(int tt = 1; tt <= t; tt++)
#ifndef ONLINE_JUDGE
#define debug(args...) fprintf(stderr,args)
#else
#define debug(args...)
#endif //ONLINE_JUDGE
template<typename T> inline T abs(T t) { return t < 0? -t : t; }
const ll modn = 1000000007;
inline ll mod(ll x) { return x % modn; }

const int MAXN = 212345;

int n, m, k;
int s[MAXN];

ll mx, x;
// less than mx, &x = 0
ll dp[31][2];

ll go(int i=30, bool lt=false){
    if(i == -1) return 1;
    ll &r = dp[i][lt];
    if(r != -1)
        return r;
    if(lt){ // curr < mx
        if(x&(1<<i)) // x tem 1
            r = go(i-1, lt);  // coloca 0
        else // x tem 0
            r = 2ll*go(i-1, lt); // coloca 0 ou 1
    }
    else{ // curr = mx
        if(mx&(1<<i)){ // mx tem 1
            if(x&(1<<i)) //x tem 1
                r = go(i-1, true); // tem que ser 0
            else // x tem 0
                r = go(i-1, true) + go(i-1, lt); // coloca 0 ou 1
        }
        else // mx tem 0
            r = go(i-1, lt);
    }
    return r;
}

ll g(ll xx, ll mxx){
    memset(dp, -1, sizeof(dp));
    x = xx;
    mx = mxx;
    return go();
}

ll solve(ll i, ll j){
    if(i == j-1){
        if(i == 0)
            return 1;
        else
            return 0;
    }
    if(i == 0)
        return 2*(j-2) + 1 + solve(1, j);
    if(i&1)
        return solve(i+1, j) + 2ll*(g(i, j-1) - g(i, i-1)); // posso trocar por g(i, i) tb, ja que i != 0
    else if(j&1)
        return solve(i, j-1) + 2ll*(g(j-1, j-1) - g(j-1, i-1));
    else
        return 3ll*solve(i/2ll, j/2ll);
}

int main (){
    scanf("%d", &n);
    for(int a=0;a<n;a++){
        ll i, j;
        scanf("%lld %lld", &i, &j);
        printf("%lld\n", solve(i, j+1));
    }
}


#include <bits/stdc++.h>
using namespace std;
#define fst first
#define snd second
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
#define pb push_back
#define for_tests(t, tt) int t; scanf("%d", &t); for(int tt = 1; tt <= t; tt++)
#ifndef ONLINE_JUDGE
#define debug(args...) fprintf(stderr,args)
#else
#define debug(args...)
#endif //ONLINE_JUDGE
template<typename T> inline T abs(T t) { return t < 0? -t : t; }
const ll modn = 1000000007;
inline ll mod(ll x) { return x % modn; }

const int MAXN = 212345;

int n, m, k;
int s[MAXN];

ll mx, x;
// less than mx, &x = 0
ll dp[31][2];

ll go(int i=30, bool lt=false){
    if(i == -1) return 1;
    ll &r = dp[i][lt];
    if(r != -1)
        return r;
    if(lt){ // curr < mx
        if(x&(1<<i)) // x tem 1
            r = go(i-1, lt);  // coloca 0
        else // x tem 0
            r = 2ll*go(i-1, lt); // coloca 0 ou 1
    }
    else{ // curr = mx
        if(mx&(1<<i)){ // mx tem 1
            if(x&(1<<i)) //x tem 1
                r = go(i-1, true); // tem que ser 0
            else // x tem 0
                r = go(i-1, true) + go(i-1, lt); // coloca 0 ou 1
        }
        else // mx tem 0
            r = go(i-1, lt);
    }
    return r;
}

ll g(ll xx, ll mxx){
    memset(dp, -1, sizeof(dp));
    x = xx;
    mx = mxx;
    return go();
}

ll solve(ll i, ll j){
    if(i == j-1){
        if(i == 0)
            return 1;
        else
            return 0;
    }
    if(i == 0)
        return 2ll*(j-1ll) + 1ll + solve(1, j);
    if(i&1)
        return solve(i+1, j) + 2ll*(g(i, j-1) - g(i, i-1)); // posso trocar por g(i, i) tb, ja que i != 0
    else if(j&1)
        return solve(i, j-1) + 2ll*(g(j-1, j-1) - g(j-1, i-1));
    else
        return 3ll*solve(i/2ll, j/2ll);
}

int main (){
    scanf("%d", &n);
    for(int a=0;a<n;a++){
        ll i, j;
        scanf("%lld %lld", &i, &j);
        printf("%lld\n", solve(i, j+1));
    }
}


#include<bits/stdc++.h>
using namespace std;

long long dp[35][2][2];
long long ans(int l,int r,int x,int sa,int sb){

    if(x==-1)return 1;
    if(dp[x][sa][sb]!=-1)return dp[x][sa][sb];
    int ma=1,mb=1;
    if(sa)ma=(l>>x)&1;
    if(sb)mb=(r>>x)&1;
    dp[x][sa][sb]=0;
    for(int i=0;i<=ma;i++){
        for(int j=0;j<=mb;j++){
            if((i&j)==0){
                dp[x][sa][sb]+=ans(l,r,x-1,sa&(i==ma),sb&(j==mb));
            }
        }
    }
    return dp[x][sa][sb];
}
long long ans(int l,int r){
    memset(dp,-1,sizeof(dp));
    return ans(l,r,30,1,1);
}
void solve(){
    int l,r;
    scanf("%d%d",&l,&r);
    cout<<ans(r,r)-2*ans(l-1,r)+ans(l-1,l-1)<<endl;
}
int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        solve();
    }
}


#include<bits/stdc++.h>
using namespace std;

long long dp[35][2][2];
long long ans(int l,int r,int x,int sa,int sb){

    if(x==-1)return 1;
    if(dp[x][sa][sb]!=-1)return dp[x][sa][sb];
    int ma=1,mb=1;
    if(sa)ma=(l>>x)&1;
    if(sb)mb=(r>>x)&1;
    dp[x][sa][sb]=0;
    for(int i=0;i<=ma;i++){
        for(int j=0;j<=mb;j++){
            if((i&j)==0){
                dp[x][sa][sb]+=ans(l,r,x-1,sa&(i==ma),sb&(j==mb));
            }
        }
    }
    return dp[x][sa][sb];
}
long long ans(int l,int r){
    if(l<0||r<0)return 0;
    memset(dp,-1,sizeof(dp));
    return ans(l,r,30,1,1);
}
void solve(){
    int l,r;
    scanf("%d%d",&l,&r);
    cout<<ans(r,r)-2*ans(l-1,r)+ans(l-1,l-1)<<endl;
}
int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        solve();
    }
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define mp make_pair
ll dp[66][2][2];
ll solve(ll a,ll b){
    if(a==-1)return 0;
    if(a==0)return b+1;
    vector<int> va,vb;
    while(a){
        va.pb(a%2);
        a/=2;
    }
    while(b){
        vb.pb(b%2);
        b/=2;
    }
    auto mx=max(va.size(),vb.size());
    while(va.size()<mx)va.pb(0);
    while(vb.size()<mx)vb.pb(0);
    memset(dp,0,sizeof dp);
    dp[mx][0][0]=1;
    for(int i=mx-1;i>=0;i--){
        for(int h=0;h<2;h++){
            for(int z=0;z<2;z++){
                //00
                dp[i][h|va[i]][z|vb[i]]+=dp[i+1][h][z];
                //01
                if(z||vb[i])dp[i][h|va[i]][z]+=dp[i+1][h][z];
                //10
                if(h||va[i])dp[i][h][z|vb[i]]+=dp[i+1][h][z];
            }
        }
    }
    return dp[0][0][0]+dp[0][0][1]+dp[0][1][0]+dp[0][1][1];
}

int main() {
    int t;
    cin>>t;
    while(t--){
        int l,r;
        cin>>l>>r;
        ll ans=solve(r,r)+solve(l-1,l-1)-solve(l-1,r)*2;
        cout<<ans<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define mp make_pair
ll dp[66][2][2];
ll solve(ll a,ll b){
    vector<int> va,vb;
    while(a){
        va.pb(a%2);
        a/=2;
    }
    while(b){
        vb.pb(b%2);
        b/=2;
    }
    auto mx=max(va.size(),vb.size());
    while(va.size()<mx)va.pb(0);
    while(vb.size()<mx)vb.pb(0);
    memset(dp,0,sizeof dp);
    dp[mx][0][0]=1;
    for(int i=mx-1;i>=0;i--){
        for(int h=0;h<2;h++){
            for(int z=0;z<2;z++){
                //00
                dp[i][h|va[i]][z|vb[i]]+=dp[i+1][h][z];
                //01
                if(z||vb[i])dp[i][h|va[i]][z]+=dp[i+1][h][z];
                //10
                if(h||va[i])dp[i][h][z|vb[i]]+=dp[i+1][h][z];
            }
        }
    }
    return dp[0][0][0]+dp[0][0][1]+dp[0][1][0]+dp[0][1][1];
}

int main() {
    int t;
    cin>>t;
    while(t--){
        int l,r;
        cin>>l>>r;
        ll ans=solve(r,r)+solve(l-1,l-1)-solve(l-1,r)*2;
        cout<<ans<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define LL long long
int f(int x,int r){//[0,r-1]
	int ans=0,w=0;
	for(int i=1;i<=r;i<<=1){
		if(r&i){
			r^=i;
			if(!(r&x))ans+=(1<<w);
		}
		if(!(x&i))w++;
	}
	return ans;
}
LL cal(int l,int r){
	LL ans=0;
	if(l==r)return 0;
	if(l==0)return 2*r-1+cal(1,r);
	if(l&1)ans+=f(l,r)-f(l,l),l++;
	if(r&1)ans+=f(r-1,r)-f(r-1,l),r--;
	return ans+cal(l/2,r/2)*3;
}
void work(){
	int a,b;scanf("%d%d",&a,&b);
	printf("%lld\n",cal(a,b+1)*2);
}
int main(){
	int t;scanf("%d",&t);
	while(t--)work();
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define LL long long
int f(int x,int r){//the num[0,r-1]
	int ans=0,w=0;
	for(int i=1;i<=r;i<<=1){
		if(r&i){
			r^=i;
			if(!(r&x))ans+=(1<<w);
		}
		if(!(x&i))w++;
	}
	return ans;
}
LL cal(int l,int r){
	LL ans=0;
	if(l==r)return 0;
	if(l==0)return 2*r-1+cal(1,r);
	if(l&1)ans+=f(l,r)-f(l,l),l++;
	if(r&1)ans+=f(r-1,r)-f(r-1,l),r--;
	return ans*2+cal(l/2,r/2)*3;
}
void work(){
	int a,b;scanf("%d%d",&a,&b);
	printf("%lld\n",cal(a,b+1));
}
int main(){
	int t;scanf("%d",&t);
	while(t--)work();
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define LL long long
int f(int x,int r){
	int ans=0,w=0;
	for(int i=1;i<=r;i<<=1){
		if(r&i){
			r^=i;
			if(!(r&x))ans+=(1<<w);
		}
		if(!(x&i))w++;
	}
	return ans;
}
LL cal(int l,int r){
	LL ans=0;
	if(l==r)return 0;
	if(l&1)ans+=f(l,r)-f(l,l),l++;
	if(r&1)ans+=f(r-1,r)-f(r-1,l),r--;
	return ans+cal(l/2,r/2)*3;
}
void work(){
	int a,b;scanf("%d%d",&a,&b);
	printf("%lld\n",cal(a,b+1)*2);
}
int main(){
	int t;scanf("%d",&t);
	while(t--)work();
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define pb push_back
#define mp make_pair
#define ALL(x) (x).begin(),(x).end()
typedef long long ll;
typedef pair<int,int> pii;
const int maxn = 3e5 + 70;
const int INF = 0x3f3f3f3f;
const ll inf = 0x3f3f3f3f3f3f3f3f;
const int MOD = 1e9 + 7;
const double eps = 1e-7;
const double PI = acos(-1.0);

int g(int x, int R){
    int ans = 0;
    int cnt = 0;
    for(int i=1;i<=R;i<<=1){
        if(R & i){
            R ^= i;
            if(!(x & R)) ans += (1 << cnt);
        }
        if(!(x & i)) cnt++;
    }
    return ans;
}

ll f(int L, int R){
    if(L == R) return 0;

    ll ans = 0;
    if(L & 1){
        ans += 2*(g(L, R) - g(L, L));
        L++;
    }
    if(R & 1){
        ans -= 2*(g(R, R) - g(R, L));
        R++;
    }

    ans += 3*f(L/2, R/2);
    return ans;
}

int main(){
    //freopen("in.txt", "r", stdin);
    int T;
    cin>>T;
    while(T--){
        int L, R;
        scanf("%d %d",&L,&R); R++;
        ll ans = f(L, R);
        printf("%lld\n",ans);
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define pb push_back
#define mp make_pair
#define ALL(x) (x).begin(),(x).end()
typedef long long ll;
typedef pair<int,int> pii;
const int maxn = 3e5 + 70;
const int INF = 0x3f3f3f3f;
const ll inf = 0x3f3f3f3f3f3f3f3f;
const int MOD = 1e9 + 7;
const double eps = 1e-7;
const double PI = acos(-1.0);

int g(int x, int R){
    int ans = 0;
    int cnt = 0;
    for(int i=1;i<=R;i<<=1){
        if(R & i){
            R ^= i;
            if(!(x & R)) ans += (1 << cnt);
        }
        if(!(x & i)) cnt++;
    }
    return ans;
}

ll f(int L, int R){
    if(L == R) return 0;

    ll ans = 0;
    if(L & 1){
        ans += 2*(g(L, R) - g(L, L));
        L++;
    }
    if(R & 1){
        ans += 2*(g(R-1, R) - g(R-1, L));
        R--;
    }

    ans += 3*f(L/2, R/2);
    return ans;
}

int main(){
    //freopen("in.txt", "r", stdin);
    int T;
    cin>>T;
    while(T--){
        int L, R;
        scanf("%d %d",&L,&R); R++;
        ll ans = f(L, R);
        printf("%lld\n",ans);
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define pb push_back
#define mp make_pair
#define ALL(x) (x).begin(),(x).end()
typedef long long ll;
typedef pair<int,int> pii;
const int maxn = 3e5 + 70;
const int INF = 0x3f3f3f3f;
const ll inf = 0x3f3f3f3f3f3f3f3f;
const int MOD = 1e9 + 7;
const double eps = 1e-7;
const double PI = acos(-1.0);

int g(int x, int R){
    int ans = 0;
    int cnt = 0;
    for(int i=1;i<=R;i<<=1){
        if(R & i){
            R ^= i;
            if(!(x & R)) ans += (1 << cnt);
        }
        if(!(x & i)) cnt++;
    }
    return ans;
}

ll f(int L, int R){
    if(L == R) return 0;
    if(L == 0) return 2*R - 1 + f(1, R);

    ll ans = 0;
    if(L & 1){
        ans += 2*(g(L, R) - g(L, L));
        L++;
    }
    if(R & 1){
        ans += 2*(g(R-1, R) - g(R-1, L));
        R--;
    }

    ans += 3*f(L/2, R/2);
    return ans;
}

int main(){
    //freopen("in.txt", "r", stdin);
    int T;
    cin>>T;
    while(T--){
        int L, R;
        scanf("%d %d",&L,&R); R++;
        ll ans = f(L, R);
        printf("%lld\n",ans);
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define pb push_back
#define mp make_pair
#define ALL(x) (x).begin(),(x).end()
typedef long long ll;
typedef pair<int,int> pii;
const int maxn = 3e5 + 70;
const int INF = 0x3f3f3f3f;
const ll inf = 0x3f3f3f3f3f3f3f3f;
const int MOD = 1e9 + 7;
const double eps = 1e-7;
const double PI = acos(-1.0);

int g(int x, int R){
    int ans = 0;
    int cnt = 0;
    for(int i=1;i<=R;i<<=1){
        if(R & i){
            R ^= i;
            if(!(x & R)) ans += (1 << cnt);
        }
        if(!(x & i)) cnt++;
    }
    return ans;
}

ll f(int L, int R){
    if(L == R) return 0;
    if(L == 0) return 2*b - 1 + f(1, R);

    ll ans = 0;
    if(L & 1){
        ans += 2*(g(L, R) - g(L, L));
        L++;
    }
    if(R & 1){
        ans += 2*(g(R-1, R) - g(R-1, L));
        R--;
    }

    ans += 3*f(L/2, R/2);
    return ans;
}

int main(){
    //freopen("in.txt", "r", stdin);
    int T;
    cin>>T;
    while(T--){
        int L, R;
        scanf("%d %d",&L,&R); R++;
        ll ans = f(L, R);
        printf("%lld\n",ans);
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
namespace fastIO{
	#define BUF_SIZE 100000
	#define OUT_SIZE 100000
	//fread->read
	bool IOerror=0;
	//inline char nc(){char ch=getchar();if(ch==-1)IOerror=1;return ch;}
	inline char nc(){
		static char buf[BUF_SIZE],*p1=buf+BUF_SIZE,*pend=buf+BUF_SIZE;
		if(p1==pend){
			p1=buf;pend=buf+fread(buf,1,BUF_SIZE,stdin);
			if(pend==p1){IOerror=1;return -1;}
		}
		return *p1++;
	}
	inline bool blank(char ch){return ch==' '||ch=='\n'||ch=='\r'||ch=='\t';}
	template<class T> inline bool read(T &x){
		bool sign=0;char ch=nc();x=0;
		for(;blank(ch);ch=nc());
		if(IOerror)return false;
		if(ch=='-')sign=1,ch=nc();
		for(;ch>='0'&&ch<='9';ch=nc())x=x*10+ch-'0';
		if(sign)x=-x;
		return true;
	}
	inline bool read(double &x){
		bool sign=0;char ch=nc();x=0;
		for(;blank(ch);ch=nc());
		if(IOerror)return false;
		if(ch=='-')sign=1,ch=nc();
		for(;ch>='0'&&ch<='9';ch=nc())x=x*10+ch-'0';
		if(ch=='.'){
			double tmp=1; ch=nc();
			for(;ch>='0'&&ch<='9';ch=nc())tmp/=10.0,x+=tmp*(ch-'0');
		}
		if(sign)x=-x;
		return true;
	}
	inline bool read(char *s){
		char ch=nc();
		for(;blank(ch);ch=nc());
		if(IOerror)return false;
		for(;!blank(ch)&&!IOerror;ch=nc())*s++=ch;
		*s=0;
		return true;
	}
	inline bool read(char &c){
		for(c=nc();blank(c);c=nc());
		if(IOerror){c=-1;return false;}
		return true;
	}
	template<class T,class... U>bool read(T& h,U&... t){return read(h)&&read(t...);}
	#undef OUT_SIZE
	#undef BUF_SIZE
};
using namespace fastIO;
/************* debug begin *************/
string to_string(string s){return '"'+s+'"';}
string to_string(const char* s){return to_string((string)s);}
string to_string(const bool& b){return(b?"true":"false");}
template<class T>string to_string(T x){ostringstream sout;sout<<x;return sout.str();}
template<class A,class B>string to_string(pair<A,B> p){return "("+to_string(p.first)+", "+to_string(p.second)+")";}
template<class A>string to_string(const vector<A> v){
	int f=1;string res="{";for(const auto x:v){if(!f)res+= ", ";f=0;res+=to_string(x);}res+="}";
	return res;
}
void debug_out(){puts("");}
template<class T,class... U>void debug_out(const T& h,const U&... t){cout<<" "<<to_string(h);debug_out(t...);}
#ifdef tokitsukaze
#define debug(...) cout<<"["<<#__VA_ARGS__<<"]:",debug_out(__VA_ARGS__);
#else
#define debug(...) 233;
#endif
/*************  debug end  *************/
#define mem(a,b) memset((a),(b),sizeof(a))
#define MP make_pair
#define pb push_back
#define fi first
#define se second
#define sz(x) (int)x.size()
#define all(x) x.begin(),x.end()
#define sqr(x) (x)*(x)
using namespace __gnu_cxx;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef pair<ll,ll> PLL;
typedef pair<int,ll> PIL;
typedef pair<ll,int> PLI;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<PII > VPII;
/************* define end  *************/
void read(int *x,int l,int r){for(int i=l;i<=r;i++) read(x[i]);}
void read(ll *x,int l,int r){for(int i=l;i<=r;i++) read(x[i]);}
void read(double *x,int l,int r){for(int i=l;i<=r;i++) read(x[i]);}
void println(VI x){for(int i=0;i<sz(x);i++) printf("%d%c",x[i]," \n"[i==sz(x)-1]);}
void println(VL x){for(int i=0;i<sz(x);i++) printf("%lld%c",x[i]," \n"[i==sz(x)-1]);}
void println(int *x,int l,int r){for(int i=l;i<=r;i++) printf("%d%c",x[i]," \n"[i==r]);}
void println(ll *x,int l,int r){for(int i=l;i<=r;i++) printf("%lld%c",x[i]," \n"[i==r]);}
/*************** IO end  ***************/
void go();
int main(){
	#ifdef tokitsukaze
		freopen("TEST.txt","r",stdin);
	#endif
	go();return 0;
}
const int INF=0x3f3f3f3f;
const ll LLINF=0x3f3f3f3f3f3f3f3fLL;
const double PI=acos(-1.0);
const double eps=1e-6;
const int MAX=2e5+10;
const ll mod=1e9+7;
/*********************************  head  *********************************/
const int DIG=35+2;
ll dp[DIG][2][2];
ll gao(ll x,ll y)
{
	const int base=2;
	int p[DIG],tot=0;
	int p2[DIG];
	if(x==-1||y==-1) return 0ll;
	while(1)
	{
		p[tot]=x%base;
		x/=base;
		p2[tot++]=y%base;
		y/=base;
		if(!x&&!y) break;
	}
	function<ll(int,int,int,int,int)> dfs=[&](int pos,int lead,int lead2,int limt,int limt2)->ll
	{
		if(pos==-1) return !lead&&!lead2;
		if(!limt&&!limt2&&dp[pos][lead][lead2]!=-1) return dp[pos][lead][lead2];
		ll res=0;
		for(int i=(limt?p[pos]:base-1);~i;i--)
		{
			for(int j=(limt2?p2[pos]:base-1);~j;j--)
			{
				if(i==1&&j==1) continue;
				res+=dfs(pos-1,lead&&i==0&&pos,lead2&&j==0&&pos,limt&&i==p[pos],limt2&&j==p2[pos]);
			}
		}
		if(!limt&&!limt2) dp[pos][lead][lead2]=res;
		return res;
	};
	return dfs(tot-1,1,1,1,1);
}
void go()
{
	int t;
	ll l,r;
	mem(dp,-1);
	read(t);
	while(t--)
	{
		read(l,r);
		printf("%lld\n",gao(r,r)-2*gao(l-1,r)+gao(l-1,l-1));
	}
}


#include <bits/stdc++.h>
using namespace std;
namespace fastIO{
	#define BUF_SIZE 100000
	#define OUT_SIZE 100000
	//fread->read
	bool IOerror=0;
	//inline char nc(){char ch=getchar();if(ch==-1)IOerror=1;return ch;}
	inline char nc(){
		static char buf[BUF_SIZE],*p1=buf+BUF_SIZE,*pend=buf+BUF_SIZE;
		if(p1==pend){
			p1=buf;pend=buf+fread(buf,1,BUF_SIZE,stdin);
			if(pend==p1){IOerror=1;return -1;}
		}
		return *p1++;
	}
	inline bool blank(char ch){return ch==' '||ch=='\n'||ch=='\r'||ch=='\t';}
	template<class T> inline bool read(T &x){
		bool sign=0;char ch=nc();x=0;
		for(;blank(ch);ch=nc());
		if(IOerror)return false;
		if(ch=='-')sign=1,ch=nc();
		for(;ch>='0'&&ch<='9';ch=nc())x=x*10+ch-'0';
		if(sign)x=-x;
		return true;
	}
	inline bool read(double &x){
		bool sign=0;char ch=nc();x=0;
		for(;blank(ch);ch=nc());
		if(IOerror)return false;
		if(ch=='-')sign=1,ch=nc();
		for(;ch>='0'&&ch<='9';ch=nc())x=x*10+ch-'0';
		if(ch=='.'){
			double tmp=1; ch=nc();
			for(;ch>='0'&&ch<='9';ch=nc())tmp/=10.0,x+=tmp*(ch-'0');
		}
		if(sign)x=-x;
		return true;
	}
	inline bool read(char *s){
		char ch=nc();
		for(;blank(ch);ch=nc());
		if(IOerror)return false;
		for(;!blank(ch)&&!IOerror;ch=nc())*s++=ch;
		*s=0;
		return true;
	}
	inline bool read(char &c){
		for(c=nc();blank(c);c=nc());
		if(IOerror){c=-1;return false;}
		return true;
	}
	template<class T,class... U>bool read(T& h,U&... t){return read(h)&&read(t...);}
	#undef OUT_SIZE
	#undef BUF_SIZE
};
using namespace fastIO;
/************* debug begin *************/
string to_string(string s){return '"'+s+'"';}
string to_string(const char* s){return to_string((string)s);}
string to_string(const bool& b){return(b?"true":"false");}
template<class T>string to_string(T x){ostringstream sout;sout<<x;return sout.str();}
template<class A,class B>string to_string(pair<A,B> p){return "("+to_string(p.first)+", "+to_string(p.second)+")";}
template<class A>string to_string(const vector<A> v){
	int f=1;string res="{";for(const auto x:v){if(!f)res+= ", ";f=0;res+=to_string(x);}res+="}";
	return res;
}
void debug_out(){puts("");}
template<class T,class... U>void debug_out(const T& h,const U&... t){cout<<" "<<to_string(h);debug_out(t...);}
#ifdef tokitsukaze
#define debug(...) cout<<"["<<#__VA_ARGS__<<"]:",debug_out(__VA_ARGS__);
#else
#define debug(...) 233;
#endif
/*************  debug end  *************/
#define mem(a,b) memset((a),(b),sizeof(a))
#define MP make_pair
#define pb push_back
#define fi first
#define se second
#define sz(x) (int)x.size()
#define all(x) x.begin(),x.end()
#define sqr(x) (x)*(x)
using namespace __gnu_cxx;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef pair<ll,ll> PLL;
typedef pair<int,ll> PIL;
typedef pair<ll,int> PLI;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<PII > VPII;
/************* define end  *************/
void read(int *x,int l,int r){for(int i=l;i<=r;i++) read(x[i]);}
void read(ll *x,int l,int r){for(int i=l;i<=r;i++) read(x[i]);}
void read(double *x,int l,int r){for(int i=l;i<=r;i++) read(x[i]);}
void println(VI x){for(int i=0;i<sz(x);i++) printf("%d%c",x[i]," \n"[i==sz(x)-1]);}
void println(VL x){for(int i=0;i<sz(x);i++) printf("%lld%c",x[i]," \n"[i==sz(x)-1]);}
void println(int *x,int l,int r){for(int i=l;i<=r;i++) printf("%d%c",x[i]," \n"[i==r]);}
void println(ll *x,int l,int r){for(int i=l;i<=r;i++) printf("%lld%c",x[i]," \n"[i==r]);}
/*************** IO end  ***************/
void go();
int main(){
	#ifdef tokitsukaze
		freopen("TEST.txt","r",stdin);
	#endif
	go();return 0;
}
const int INF=0x3f3f3f3f;
const ll LLINF=0x3f3f3f3f3f3f3f3fLL;
const double PI=acos(-1.0);
const double eps=1e-6;
const int MAX=2e5+10;
const ll mod=1e9+7;
/*********************************  head  *********************************/
const int DIG=35+2;
ll dp[DIG][2][2][2][2];
ll gao(ll x,ll y)
{
	mem(dp,-1);
	const int base=2;
	int p[DIG],tot=0;
	int p2[DIG];
	if(x==-1||y==-1) return 0ll;
	while(1)
	{
		p[tot]=x%base;
		x/=base;
		p2[tot++]=y%base;
		y/=base;
		if(!x&&!y) break;
	}
	function<ll(int,int,int,int,int)> dfs=[&](int pos,int lead,int lead2,int limt,int limt2)->ll
	{
		if(pos==-1) return !lead&&!lead2;
		if(dp[pos][limt][limt2][lead][lead2]!=-1) return dp[pos][limt][limt2][lead][lead2];
		ll res=0;
		for(int i=(limt?p[pos]:base-1);~i;i--)
		{
			for(int j=(limt2?p2[pos]:base-1);~j;j--)
			{
				if(i==1&&j==1) continue;
				res+=dfs(pos-1,lead&&i==0&&pos,lead2&&j==0&&pos,limt&&i==p[pos],limt2&&j==p2[pos]);
			}
		}
		dp[pos][limt][limt2][lead][lead2]=res;
		return res;
	};
	return dfs(tot-1,1,1,1,1);
}
void go()
{
	int t;
	ll l,r;
	read(t);
	while(t--)
	{
		read(l,r);
		printf("%lld\n",gao(r,r)-2*gao(l-1,r)+gao(l-1,l-1));
	}
}


#include <bits/stdc++.h>
using namespace std;
namespace fastIO{
	#define BUF_SIZE 100000
	#define OUT_SIZE 100000
	//fread->read
	bool IOerror=0;
	//inline char nc(){char ch=getchar();if(ch==-1)IOerror=1;return ch;}
	inline char nc(){
		static char buf[BUF_SIZE],*p1=buf+BUF_SIZE,*pend=buf+BUF_SIZE;
		if(p1==pend){
			p1=buf;pend=buf+fread(buf,1,BUF_SIZE,stdin);
			if(pend==p1){IOerror=1;return -1;}
		}
		return *p1++;
	}
	inline bool blank(char ch){return ch==' '||ch=='\n'||ch=='\r'||ch=='\t';}
	template<class T> inline bool read(T &x){
		bool sign=0;char ch=nc();x=0;
		for(;blank(ch);ch=nc());
		if(IOerror)return false;
		if(ch=='-')sign=1,ch=nc();
		for(;ch>='0'&&ch<='9';ch=nc())x=x*10+ch-'0';
		if(sign)x=-x;
		return true;
	}
	inline bool read(double &x){
		bool sign=0;char ch=nc();x=0;
		for(;blank(ch);ch=nc());
		if(IOerror)return false;
		if(ch=='-')sign=1,ch=nc();
		for(;ch>='0'&&ch<='9';ch=nc())x=x*10+ch-'0';
		if(ch=='.'){
			double tmp=1; ch=nc();
			for(;ch>='0'&&ch<='9';ch=nc())tmp/=10.0,x+=tmp*(ch-'0');
		}
		if(sign)x=-x;
		return true;
	}
	inline bool read(char *s){
		char ch=nc();
		for(;blank(ch);ch=nc());
		if(IOerror)return false;
		for(;!blank(ch)&&!IOerror;ch=nc())*s++=ch;
		*s=0;
		return true;
	}
	inline bool read(char &c){
		for(c=nc();blank(c);c=nc());
		if(IOerror){c=-1;return false;}
		return true;
	}
	template<class T,class... U>bool read(T& h,U&... t){return read(h)&&read(t...);}
	#undef OUT_SIZE
	#undef BUF_SIZE
};
using namespace fastIO;
/************* debug begin *************/
string to_string(string s){return '"'+s+'"';}
string to_string(const char* s){return to_string((string)s);}
string to_string(const bool& b){return(b?"true":"false");}
template<class T>string to_string(T x){ostringstream sout;sout<<x;return sout.str();}
template<class A,class B>string to_string(pair<A,B> p){return "("+to_string(p.first)+", "+to_string(p.second)+")";}
template<class A>string to_string(const vector<A> v){
	int f=1;string res="{";for(const auto x:v){if(!f)res+= ", ";f=0;res+=to_string(x);}res+="}";
	return res;
}
void debug_out(){puts("");}
template<class T,class... U>void debug_out(const T& h,const U&... t){cout<<" "<<to_string(h);debug_out(t...);}
#ifdef tokitsukaze
#define debug(...) cout<<"["<<#__VA_ARGS__<<"]:",debug_out(__VA_ARGS__);
#else
#define debug(...) 233;
#endif
/*************  debug end  *************/
#define mem(a,b) memset((a),(b),sizeof(a))
#define MP make_pair
#define pb push_back
#define fi first
#define se second
#define sz(x) (int)x.size()
#define all(x) x.begin(),x.end()
#define sqr(x) (x)*(x)
using namespace __gnu_cxx;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef pair<ll,ll> PLL;
typedef pair<int,ll> PIL;
typedef pair<ll,int> PLI;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<PII > VPII;
/************* define end  *************/
void read(int *x,int l,int r){for(int i=l;i<=r;i++) read(x[i]);}
void read(ll *x,int l,int r){for(int i=l;i<=r;i++) read(x[i]);}
void read(double *x,int l,int r){for(int i=l;i<=r;i++) read(x[i]);}
void println(VI x){for(int i=0;i<sz(x);i++) printf("%d%c",x[i]," \n"[i==sz(x)-1]);}
void println(VL x){for(int i=0;i<sz(x);i++) printf("%lld%c",x[i]," \n"[i==sz(x)-1]);}
void println(int *x,int l,int r){for(int i=l;i<=r;i++) printf("%d%c",x[i]," \n"[i==r]);}
void println(ll *x,int l,int r){for(int i=l;i<=r;i++) printf("%lld%c",x[i]," \n"[i==r]);}
/*************** IO end  ***************/
void go();
int main(){
	#ifdef tokitsukaze
		freopen("TEST.txt","r",stdin);
	#endif
	go();return 0;
}
const int INF=0x3f3f3f3f;
const ll LLINF=0x3f3f3f3f3f3f3f3fLL;
const double PI=acos(-1.0);
const double eps=1e-6;
const int MAX=2e5+10;
const ll mod=1e9+7;
/*********************************  head  *********************************/
const int DIG=35+2;
ll dp[DIG][2][2];
ll gao(ll x,ll y)
{
	mem(dp,-1);
	const int base=2;
	int p[DIG],tot=0;
	int p2[DIG];
	if(x==-1||y==-1) return 0ll;
	while(1)
	{
		p[tot]=x%base;
		x/=base;
		p2[tot++]=y%base;
		y/=base;
		if(!x&&!y) break;
	}
	function<ll(int,int,int,int,int)> dfs=[&](int pos,int lead,int lead2,int limt,int limt2)->ll
	{
		if(pos==-1) return !lead&&!lead2;
		if(!limt&&!limt2&&dp[pos][lead][lead2]!=-1) return dp[pos][lead][lead2];
		ll res=0;
		for(int i=(limt?p[pos]:base-1);~i;i--)
		{
			for(int j=(limt2?p2[pos]:base-1);~j;j--)
			{
				if(i==1&&j==1) continue;
				res+=dfs(pos-1,lead&&i==0&&pos,lead2&&j==0&&pos,limt&&i==p[pos],limt2&&j==p2[pos]);
			}
		}
		if(!limt&&!limt2) dp[pos][lead][lead2]=res;
		return res;
	};
	return dfs(tot-1,1,1,1,1);
}
void go()
{
	int t;
	ll l,r;
	read(t);
	while(t--)
	{
		read(l,r);
		printf("%lld\n",gao(r,r)-2*gao(l-1,r)+gao(l-1,l-1));
	}
}


#include <bits/stdc++.h>
using namespace std;
namespace fastIO{
	#define BUF_SIZE 100000
	#define OUT_SIZE 100000
	//fread->read
	bool IOerror=0;
	//inline char nc(){char ch=getchar();if(ch==-1)IOerror=1;return ch;}
	inline char nc(){
		static char buf[BUF_SIZE],*p1=buf+BUF_SIZE,*pend=buf+BUF_SIZE;
		if(p1==pend){
			p1=buf;pend=buf+fread(buf,1,BUF_SIZE,stdin);
			if(pend==p1){IOerror=1;return -1;}
		}
		return *p1++;
	}
	inline bool blank(char ch){return ch==' '||ch=='\n'||ch=='\r'||ch=='\t';}
	template<class T> inline bool read(T &x){
		bool sign=0;char ch=nc();x=0;
		for(;blank(ch);ch=nc());
		if(IOerror)return false;
		if(ch=='-')sign=1,ch=nc();
		for(;ch>='0'&&ch<='9';ch=nc())x=x*10+ch-'0';
		if(sign)x=-x;
		return true;
	}
	inline bool read(double &x){
		bool sign=0;char ch=nc();x=0;
		for(;blank(ch);ch=nc());
		if(IOerror)return false;
		if(ch=='-')sign=1,ch=nc();
		for(;ch>='0'&&ch<='9';ch=nc())x=x*10+ch-'0';
		if(ch=='.'){
			double tmp=1; ch=nc();
			for(;ch>='0'&&ch<='9';ch=nc())tmp/=10.0,x+=tmp*(ch-'0');
		}
		if(sign)x=-x;
		return true;
	}
	inline bool read(char *s){
		char ch=nc();
		for(;blank(ch);ch=nc());
		if(IOerror)return false;
		for(;!blank(ch)&&!IOerror;ch=nc())*s++=ch;
		*s=0;
		return true;
	}
	inline bool read(char &c){
		for(c=nc();blank(c);c=nc());
		if(IOerror){c=-1;return false;}
		return true;
	}
	template<class T,class... U>bool read(T& h,U&... t){return read(h)&&read(t...);}
	#undef OUT_SIZE
	#undef BUF_SIZE
};
using namespace fastIO;
/************* debug begin *************/
string to_string(string s){return '"'+s+'"';}
string to_string(const char* s){return to_string((string)s);}
string to_string(const bool& b){return(b?"true":"false");}
template<class T>string to_string(T x){ostringstream sout;sout<<x;return sout.str();}
template<class A,class B>string to_string(pair<A,B> p){return "("+to_string(p.first)+", "+to_string(p.second)+")";}
template<class A>string to_string(const vector<A> v){
	int f=1;string res="{";for(const auto x:v){if(!f)res+= ", ";f=0;res+=to_string(x);}res+="}";
	return res;
}
void debug_out(){puts("");}
template<class T,class... U>void debug_out(const T& h,const U&... t){cout<<" "<<to_string(h);debug_out(t...);}
#ifdef tokitsukaze
#define debug(...) cout<<"["<<#__VA_ARGS__<<"]:",debug_out(__VA_ARGS__);
#else
#define debug(...) 233;
#endif
/*************  debug end  *************/
#define mem(a,b) memset((a),(b),sizeof(a))
#define MP make_pair
#define pb push_back
#define fi first
#define se second
#define sz(x) (int)x.size()
#define all(x) x.begin(),x.end()
#define sqr(x) (x)*(x)
using namespace __gnu_cxx;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef pair<ll,ll> PLL;
typedef pair<int,ll> PIL;
typedef pair<ll,int> PLI;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<PII > VPII;
/************* define end  *************/
void read(int *x,int l,int r){for(int i=l;i<=r;i++) read(x[i]);}
void read(ll *x,int l,int r){for(int i=l;i<=r;i++) read(x[i]);}
void read(double *x,int l,int r){for(int i=l;i<=r;i++) read(x[i]);}
void println(VI x){for(int i=0;i<sz(x);i++) printf("%d%c",x[i]," \n"[i==sz(x)-1]);}
void println(VL x){for(int i=0;i<sz(x);i++) printf("%lld%c",x[i]," \n"[i==sz(x)-1]);}
void println(int *x,int l,int r){for(int i=l;i<=r;i++) printf("%d%c",x[i]," \n"[i==r]);}
void println(ll *x,int l,int r){for(int i=l;i<=r;i++) printf("%lld%c",x[i]," \n"[i==r]);}
/*************** IO end  ***************/
void go();
int main(){
	#ifdef tokitsukaze
		freopen("TEST.txt","r",stdin);
	#endif
	go();return 0;
}
const int INF=0x3f3f3f3f;
const ll LLINF=0x3f3f3f3f3f3f3f3fLL;
const double PI=acos(-1.0);
const double eps=1e-6;
const int MAX=2e5+10;
const ll mod=1e9+7;
/*********************************  head  *********************************/
const int DIG=35+2;
ll dp[DIG][2][2];
ll gao(ll x,ll y)
{
	mem(dp,-1);
	const int base=2;
	int p[DIG],tot=0;
	int p2[DIG];
	while(1)
	{
		p[tot]=x%base;
		x/=base;
		p2[tot++]=y%base;
		y/=base;
		if(!x&&!y) break;
	}
	function<ll(int,int,int,int,int)> dfs=[&](int pos,int lead,int lead2,int limt,int limt2)->ll
	{
		if(pos==-1) return !lead&&!lead2;
		if(!limt&&!limt2&&dp[pos][lead][lead2]!=-1) return dp[pos][lead][lead2];
		ll res=0;
		for(int i=(limt?p[pos]:base-1);~i;i--)
		{
			for(int j=(limt2?p2[pos]:base-1);~j;j--)
			{
				if(i==1&&j==1) continue;
				res+=dfs(pos-1,lead&&i==0&&pos,lead2&&j==0&&pos,limt&&i==p[pos],limt2&&j==p2[pos]);
			}
		}
		if(!limt&&!limt2) dp[pos][lead][lead2]=res;
		return res;
	};
	return dfs(tot-1,1,1,1,1);
}
void go()
{
	int t;
	ll l,r;
	read(t);
	while(t--)
	{
		read(l,r);
		printf("%lld\n",gao(r,r)-2*gao(l-1,r)+gao(l-1,l-1));
	}
}


//CF 1245F

#include <bits/stdc++.h>
#define rep(i,a,b) for(int i = (a); i <= (b); ++ i)
using namespace std;

typedef long long ll;

int T;
ll ct1(ll v) {
    ll ans = 0;
    while(v) ans += (v&1), v >>= 1;
    return ans;
}
ll g(ll v,ll p) {
    ll ans = 0;
    for(ll tt,L,i = p; i; i -= i&-i) {
        tt = (i&-i); L = i - tt;
        if(!(L&v)) ans += 1 << (ct1(tt-1)-ct1((v&(tt-1))));
    }
    return ans;
}

ll f(ll l, ll r) {
    if(l == r) return 0;
    if(l == 0) return 2*r-1 + f(1,r);
    if(l & 1) return f(l+1,r) + 2ll*(g(l,r) - g(l,l));
    else if(r & 1) return f(l,r-1) + 2ll*(g(r-1,r) - g(r-1,l));
    else return f(l/2, r/2) * 3ll;
}

int main() {
    cin >> T;
    while(T--) {
        ll l, r;
        cin >> l >> r;
        cout << f(l,r+1) << '\n';
    }
}


//CF 1245F

#include <bits/stdc++.h>
#define rep(i,a,b) for(int i = (a); i <= (b); ++ i)
using namespace std;

typedef long long ll;

int T;
ll ct1(ll v) {
    ll ans = 0;
    while(v) ans += (v&1), v >>= 1;
    return ans;
}
ll g(ll v,ll p) {
    ll ans = 0;
    for(ll tt,L,i = p; i; i -= i&-i) {
        tt = (i&-i); L = i - tt;
        if(!(L&v)) ans += 1 << (ct1(tt-1)-ct1((v&(tt-1))));
    }
    return ans;
}
ll f(ll l, ll r) {
    if(l == r) return 0;
    if(l & 1) return f(l+1,r) + 2ll*(g(l,r) - g(l,l));
    else if(r & 1) return f(l,r-1) + 2ll*(g(r-1,r) - g(r-1,l));
    else return f(l/2, r/2) * 3ll;
}
int main() {
    cin >> T;
    while(T--) {
        ll l, r;
        cin >> l >> r;
        cout << f(l,r+1) << '\n';
    }
}


//CF 1245F

#include <bits/stdc++.h>
#define rep(i,a,b) for(int i = (a); i <= (b); ++ i)
using namespace std;

typedef long long ll;

int T;
ll ct1(ll v) {
    ll ans = 0;
    while(v) ans += (v&1), v >>= 1;
    return ans;
}
ll g(ll v,ll p) {
    ll ans = 0;
    for(ll tt,L,i = p; i; i -= i&-i) {
        tt = (i&-i); L = i - tt;
        if(!(L&v)) ans += 1 << (ct1(tt-1)-ct1((v&(tt-1))));
    }
    return ans;
}
ll f(ll l, ll r) {
    if(l + 1 == r) return !l;
    if(l == r) return 0;
    if(l & 1) return f(l+1,r) + 2ll*(g(l,r) - g(l,l));
    else if(r & 1) return f(l,r-1) + 2ll*(g(r-1,r) - g(r-1,l));
    else return f(l/2, r/2) * 3ll;
}
int main() {
    cin >> T;
    while(T--) {
        ll l, r;
        cin >> l >> r;
        cout << f(l,r+1) << '\n';
    }
}


#include "bits/stdc++.h"
using namespace std;

#define FAST ios_base::sync_with_stdio(false); cin.tie(0);
#define pb push_back
#define eb emplace_back
#define ins insert
#define ph push
#define f first
#define s second
#define cbr cerr << "hi\n"
#define mmst(x, v) memset((x), v, sizeof ((x)))
#define siz(x) ((ll)x.size())
#define all(x) (x).begin(), (x).end()
#define lbd(x, y) lower_bound(all(x), y)
#define ubd(x, y) upper_bound(all(x), y)
#define btinpct(x) __builtin_popcountll((x))
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());    //can be used by calling rng() or shuffle(A, A+n, rng)
inline long long rand(long long x, long long y) { return (rng() % (y+1-x)) + x; } //inclusivesss
string inline to_string(char c) {string s(1,c);return s;} template<typename T> inline T gcd(T a,T b){ return a==0?b:gcd(b%a,a); }

#define ll long long int
#define ld long double
#define FOR(ii, ss, ee) for(ll ii = (ss); ii <= (ll)(ee); ++ii)
#define DEC(ii, ss, ee) for(ll ii = (ss); ii >= (ll)(ee); --ii)
typedef pair <ll, ll> pi; typedef pair <ll, pi> spi; typedef pair <pi, pi> dpi;

#define LLINF ((long long) 1e18)//1234567890987654321
#define INF 1234567890ll
// #define cerr if(0)cout
#define MAXN (300006)
ll t, dp[31][2][2][2][2];
void solve() {
	ll l, r; cin>>l>>r;
	mmst(dp, 0);
	dp[30][1][1][1][1]=1;
	DEC(i,29,0) {
		ll lb=bool(l&(1<<i));
		ll rb=bool(r&(1<<i));
		FOR(d1,0,1) FOR(d2,0,1) FOR(s1l,0,1) FOR(s1r,0,1) FOR(s2l,0,1) FOR(s2r,0,1) {
			if(s1l && d1 < lb) continue;
			if(s1r && d1 > rb) continue;
			if(s2l && d2 < lb) continue;
			if(s2r && d2 > rb) continue;
			if(d1 == 1 && d2 == 1) continue;
			dp[i][s1l && (d1==lb)][s1r && (d1==rb)][s2l && (d2==lb)][s2r && (d2==rb)] += dp[i+1][s1l][s1r][s2l][s2r];
		}
	}
	// FOR(a,0,1) FOR(b,0,1) FOR(c,0,1) FOR(d,0,1) if(dp[0][a][b][c][d]) cerr<<a<<' '<<b<<' '<<c<<' '<<d<<'\n';
	ll ans = 0;
	FOR(a,0,1) FOR(b,0,1) FOR(c,0,1) FOR(d,0,1) ans += dp[0][a][b][c][d];
	cout<<ans<<'\n';
}
int main()
{
	FAST
	cin>>t;
	while(t--) {
		solve();
	}
}


#include "bits/stdc++.h"
using namespace std;

#define FAST ios_base::sync_with_stdio(false); cin.tie(0);
#define pb push_back
#define eb emplace_back
#define ins insert
#define ph push
#define f first
#define s second
#define cbr cerr << "hi\n"
#define mmst(x, v) memset((x), v, sizeof ((x)))
#define siz(x) ((ll)x.size())
#define all(x) (x).begin(), (x).end()
#define lbd(x, y) lower_bound(all(x), y)
#define ubd(x, y) upper_bound(all(x), y)
#define btinpct(x) __builtin_popcountll((x))
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());    //can be used by calling rng() or shuffle(A, A+n, rng)
inline long long rand(long long x, long long y) { return (rng() % (y+1-x)) + x; } //inclusivesss
string inline to_string(char c) {string s(1,c);return s;} template<typename T> inline T gcd(T a,T b){ return a==0?b:gcd(b%a,a); }

#define ll long long int
#define ld long double
#define FOR(ii, ss, ee) for(ll ii = (ss); ii <= (ll)(ee); ++ii)
#define DEC(ii, ss, ee) for(ll ii = (ss); ii >= (ll)(ee); --ii)
typedef pair <ll, ll> pi; typedef pair <ll, pi> spi; typedef pair <pi, pi> dpi;

#define LLINF ((long long) 1e18)//1234567890987654321
#define INF 1234567890ll
// #define cerr if(0)cout
#define MAXN (300006)
ll f(ll x,ll n) { // x & y = 0, and y <= n
	if(n<0) return 0;
	ll ans = ((x&n)==0), on = 0;
	for(int i=0;(1<<i)<=n;++i) {
		if(n&(1<<i)) {
			n ^= (1<<i);
			if((x&n)==0) {
				ans += 1<<on;
			}
		}
		if(((1<<i)&x)==0) ++ on;
	}
	return ans;
}
ll dp(ll x,ll y) {
	if(x>y) return 0;
	if(x==0) {
		// return 2*(y+1)-1+dp(1, y);
	}
	ll ret = 0;
	if(x&1) {
		ret+=2*(f(x, y)-f(x, x-1));
		++ x;
	}
	if((y&1)==0) {
		ret+=2*(f(y, y)-f(y, x-1));
		-- y;
	}
	ll xx=dp(x>>1, y>>1);
	return ret + 3 * xx;
}
int main()
{
	FAST
	ll t; cin>>t;
	auto solve=[&]() {
		ll l, r; cin>>l>>r;
		// ll ans=0;
		// FOR(i,l,r) FOR(j,l,r) ans += ((i+j)==(i^j));
		// cout<<ans<<' ';
		cout<<dp(l, r)<<'\n';
	};
	while(t--) {
		solve();
	}
}
/*
1
1 4
*/


#include "bits/stdc++.h"
using namespace std;

#define FAST ios_base::sync_with_stdio(false); cin.tie(0);
#define pb push_back
#define eb emplace_back
#define ins insert
#define ph push
#define f first
#define s second
#define cbr cerr << "hi\n"
#define mmst(x, v) memset((x), v, sizeof ((x)))
#define siz(x) ((ll)x.size())
#define all(x) (x).begin(), (x).end()
#define lbd(x, y) lower_bound(all(x), y)
#define ubd(x, y) upper_bound(all(x), y)
#define btinpct(x) __builtin_popcountll((x))
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());    //can be used by calling rng() or shuffle(A, A+n, rng)
inline long long rand(long long x, long long y) { return (rng() % (y+1-x)) + x; } //inclusivesss
string inline to_string(char c) {string s(1,c);return s;} template<typename T> inline T gcd(T a,T b){ return a==0?b:gcd(b%a,a); }

#define ll long long int
#define ld long double
#define FOR(ii, ss, ee) for(ll ii = (ss); ii <= (ll)(ee); ++ii)
#define DEC(ii, ss, ee) for(ll ii = (ss); ii >= (ll)(ee); --ii)
typedef pair <ll, ll> pi; typedef pair <ll, pi> spi; typedef pair <pi, pi> dpi;

#define LLINF ((long long) 1e18)//1234567890987654321
#define INF 1234567890ll
// #define cerr if(0)cout
#define MAXN (300006)
ll f(ll x,ll n) { // x & y = 0, and y <= n
	if(n<0) return 0;
	ll ans = ((x&n)==0), on = 0;
	for(int i=0;(1<<i)<=n;++i) {
		if(n&(1<<i)) {
			n ^= (1<<i);
			if((x&n)==0) {
				ans += 1<<on;
			}
		}
		if(((1<<i)&x)==0) ++ on;
	}
	return ans;
}
ll dp(ll x,ll y) {
	if(x>y) return 0;
	if(x==0) {
		return 2*(y+1)-1+dp(1, y);
	}
	ll ret = 0;
	if(x&1) {
		ret+=2*(f(x, y)-f(x, x-1));
		++ x;
	}
	if((y&1)==0) {
		ret+=2*(f(y, y)-f(y, x-1));
		-- y;
	}
	ll xx=dp(x>>1, y>>1);
	return ret + 3 * xx;
}
int main()
{
	FAST
	ll t; cin>>t;
	auto solve=[&]() {
		ll l, r; cin>>l>>r;
		// ll ans=0;
		// FOR(i,l,r) FOR(j,l,r) ans += ((i+j)==(i^j));
		// cout<<ans<<' ';
		cout<<dp(l, r)<<'\n';
	};
	while(t--) {
		solve();
	}
}
/*
1
1 4
*/


#include "bits/stdc++.h"
using namespace std;

#define FAST ios_base::sync_with_stdio(false); cin.tie(0);
#define pb push_back
#define eb emplace_back
#define ins insert
#define ph push
#define f first
#define s second
#define cbr cerr << "hi\n"
#define mmst(x, v) memset((x), v, sizeof ((x)))
#define siz(x) ((ll)x.size())
#define all(x) (x).begin(), (x).end()
#define lbd(x, y) lower_bound(all(x), y)
#define ubd(x, y) upper_bound(all(x), y)
#define btinpct(x) __builtin_popcountll((x))
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());    //can be used by calling rng() or shuffle(A, A+n, rng)
inline long long rand(long long x, long long y) { return (rng() % (y+1-x)) + x; } //inclusivesss
string inline to_string(char c) {string s(1,c);return s;} template<typename T> inline T gcd(T a,T b){ return a==0?b:gcd(b%a,a); }

#define ll long long int
#define ld long double
#define FOR(ii, ss, ee) for(ll ii = (ss); ii <= (ll)(ee); ++ii)
#define DEC(ii, ss, ee) for(ll ii = (ss); ii >= (ll)(ee); --ii)
typedef pair <ll, ll> pi; typedef pair <ll, pi> spi; typedef pair <pi, pi> dpi;

#define LLINF ((long long) 1e18)//1234567890987654321
#define INF 1234567890ll
// #define cerr if(0)cout
#define MAXN (300006)
ll f(ll x,ll n) { // x & y = 0, and y <= n
	if(n<0) return 0;
	ll ans = ((x&n)==0), on = 0;
	for(int i=0;(1<<i)<=n;++i) {
		if(n&(1<<i)) {
			n ^= (1<<i);
			if((x&n)==0) {
				ans += 1<<on;
			}
		}
		if(((1<<i)&x)==0) ++ on;
	}
	return ans;
}
ll dp(ll x,ll y) {
	if(x>y) return 0;
	if(x==0) {
		// return 2*(y+1)-1+dp(1, y);
	}
	ll ret = 0;
	if(x&1) {
		ret+=2*(f(x, y)-f(x, x-1));
		++ x;
	}
	if((y&1)==0) {
		ret+=2*(f(y, y)-f(y, x-1)) - (y==0);
		-- y;
	}
	ll xx=dp(x>>1, y>>1);
	return ret + 3 * xx;
}
int main()
{
	FAST
	ll t; cin>>t;
	auto solve=[&]() {
		ll l, r; cin>>l>>r;
		// ll ans=0;
		// FOR(i,l,r) FOR(j,l,r) ans += ((i+j)==(i^j));
		// cout<<ans<<' ';
		cout<<dp(l, r)<<'\n';
	};
	while(t--) {
		solve();
	}
}
/*
1
1 4
*/


#include<bits/stdc++.h>
using namespace std;
#define ll long long

ll dp[55][2][2][2][2];
int a[55], b[55];

ll dfs(int pos, int limit, int x, int y, int l1, int l2) {
//    cout << pos << " " << x << " " << y << "\n";
    if(limit) return 0;
    if(pos == 0) return 1;
    if(dp[pos][x][y][l1][l2] != -1) return dp[pos][x][y][l1][l2];
    int r1 = l1 ? a[pos] : 1;
    int r2 = l2 ? b[pos] : 1;
    ll ret = 0;
    for(int i = 0; i <= r1; i++)
        for(int j = 0; j <= r2; j++) {
            if(i && j) continue;
            ret += dfs(pos - 1, (i & j) |  limit, x | i, y | j, l1 && i == r1, l2 && j == r2);
        }
    return dp[pos][x][y][l1][l2] = ret;
}

ll sl(int l, int r) {
    if(l < 0 || r < 0) return 0;
//    if(l == 0) return r;
//    cout << l << " " << r << " : " ;
    int c1 = 0, c2 = 0;
    memset(a, 0, sizeof(a));
    memset(b, 0, sizeof(b));
    while(l) {
        a[++c1] = l % 2;
        l >>= 1;
    }
    while(r) {
        b[++c2] = r % 2;
        r >>= 1;
    }
    return dfs(c2, 0, 0, 0, 1, 1) ;
}

int main() {
    #ifndef ONLINE_JUDGE
    freopen("c.txt", "r", stdin);
    #endif // ONLINE_JUDGE
    int T;
    memset(dp, -1, sizeof(dp));
    scanf("%d", &T);
    while(T--) {
        int l, r;
        scanf("%d %d", &l, &r);
        printf("%lld\n", sl(r, r) - sl(l-1, r) * 2 + sl(l-1, l-1));
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long

ll dp[37];
int a[37], b[37];

ll dfs(int pos,  int l1, int l2) {
    if(pos == 0) return 1;
    if(!l1 && !l2 && dp[pos] != -1) return dp[pos];
    int r1 = l1 ? a[pos] : 1;
    int r2 = l2 ? b[pos] : 1;
    ll ret = 0;
    for(int i = 0; i <= r1; i++)
        for(int j = 0; j <= r2; j++) {
            if(i && j) continue;
            ret += dfs(pos - 1, l1 && i == r1, l2 && j == r2);
        }
    if(!l1 && !l2) dp[pos] = ret;
    return ret;
}

ll sl(int l, int r) {
    if(l < 0 || r < 0) return 0;
    int c1 = 0, c2 = 0;
    while(l) {
        a[++c1] = l % 2;
        l >>= 1;
    }
    while(r) {
        b[++c2] = r % 2;
        r >>= 1;
    }
    ll num = 0;
    for(int i = c2; i > c1; i--) num = num * 2 + b[i];
    ll v = dfs(c1, 1, 1);
    for(int i = 1; i <= c1; i++) b[i] = 1;
    v += num * dfs(c1, 1, 1);
    return v ;
}

int main() {
    #ifndef ONLINE_JUDGE
    freopen("c.txt", "r", stdin);
    #endif // ONLINE_JUDGE
    int T;
    memset(dp, -1, sizeof(dp));
    scanf("%d", &T);
    while(T--) {
        int l, r;
        scanf("%d %d", &l, &r);
        printf("%lld\n", sl(r, r) - sl(l-1, r) * 2 + sl(l-1, l-1));
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long

ll dp[55][2][2];
int a[55], b[55];

ll dfs(int pos, int limit, int l1, int l2) {
    if(limit) return 0;
    if(pos == 0) return 1;
    if(dp[pos][l1][l2] != -1) return dp[pos][l1][l2];
    int r1 = l1 ? a[pos] : 1;
    int r2 = l2 ? b[pos] : 1;
    ll ret = 0;
    for(int i = 0; i <= r1; i++)
        for(int j = 0; j <= r2; j++) {
            ret += dfs(pos - 1, (i & j) | limit, l1 && i == r1, l2 && j == r2);
        }
    return dp[pos][l1][l2] = ret;
}

ll sl(int l, int r) {
    if(l < 0 || r < 0) return 0;
    int c1 = 0, c2 = 0;
    memset(dp, -1, sizeof(dp));
    memset(a, 0, sizeof(a));
    memset(b, 0, sizeof(b));
    while(l) {
        a[++c1] = l % 2;
        l >>= 1;
    }
    while(r) {
        b[++c2] = r % 2;
        r >>= 1;
    }
    return dfs(c2,  0, 1, 1) ;
}

int main() {
    #ifndef ONLINE_JUDGE
    freopen("c.txt", "r", stdin);
    #endif // ONLINE_JUDGE
    int T;

    scanf("%d", &T);
    while(T--) {
        int l, r;
        scanf("%d %d", &l, &r);
        printf("%lld\n", sl(r, r) - sl(l-1, r) * 2 + sl(l-1, l-1));
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long

ll dp[37][2][2];
int a[37], b[37];

ll dfs(int pos, int l1, int l2) {
    if(pos == 0) return 1;
    if(dp[pos][l1][l2] != -1) return dp[pos][l1][l2];
    int r1 = l1 ? a[pos] : 1;
    int r2 = l2 ? b[pos] : 1;
    ll ret = 0;
    for(int i = 0; i <= r1; i++)
        for(int j = 0; j <= r2; j++) {
            if(i && j) continue;
            ret += dfs(pos - 1, l1 && i == r1, l2 && j == r2);
        }
    dp[pos][l1][l2] = ret;
    return ret;
}

ll sl(int l, int r) {
    if(l < 0 || r < 0) return 0;
    int c1 = 0, c2 = 0;
    memset(dp, -1, sizeof(dp));
    memset(a, 0, sizeof(a));
    memset(b, 0, sizeof(b));
    while(l) {
        a[++c1] = l % 2;
        l >>= 1;
    }
    while(r) {
        b[++c2] = r % 2;
        r >>= 1;
    }

    return dfs(c2, 1, 1);
}

int main() {
    #ifndef ONLINE_JUDGE
    freopen("c.txt", "r", stdin);
    #endif // ONLINE_JUDGE
    int T;
    scanf("%d", &T);
    while(T--) {
        int l, r;
        scanf("%d %d", &l, &r);
        printf("%lld\n", sl(r, r) - sl(l-1, r) * 2 + sl(l-1, l-1));
    }
    return 0;
}


#include <bits/stdc++.h>

#define INF 1e9
#define EPS 1e-7
#define pb push_back
#define pause system("pause");
#define exit exit(0)

using namespace std;
using ll = long long;
using ull = unsigned long long;

int get(int l, int r) {
	int res = 0, zeros = 0;

	for (int i = 1; i <= r; i <<= 1) {
		if (r & i) {
			r ^= i;

			if (!(l & r)) {
				res += 1 << zeros;
			}
		}

		if (!(l & i)) {
			++zeros;
		}
	}

	return res;
}

ll solve(int l, int r) {
	if (r - l < 1) {
		return 0;
	}

	if (l == 0) {
		return 2 * r - 1 + solve(1, r);
	}

	ll res = 0;
	if (l & 1) {
		res += 2 * (get(l, r) - get(l, l));
		++l;
	}

	if (r & 1) {
		res += 2 * (get(r - 1, r) - get(r - 1, l));
		--r;
	}

	return res + 3 * solve(l >> 1, r >> 1);
}

int t;

int main() {
	cin >> t;

	int l, r;
	while (t--) {
		cin >> l >> r;
		cout << solve(l, r + 1) << endl;
	}
	//pause;
	return 0;
}


#include <bits/stdc++.h>

#define INF 1e9
#define EPS 1e-7
#define pb push_back
#define pause system("pause");
#define exit exit(0)

using namespace std;
using ll = long long;
using ull = unsigned long long;

int get(int l, int r) {
	int res = 0, zeros = 0;

	for (int i = 1; i <= r; i <<= 1) {
		if (r & i) {
			r ^= i;

			if (!(l & r)) {
				res += 1 << zeros;
			}
		}

		if (!(l & i)) {
			++zeros;
		}
	}

	return res;
}

ll solve(int l, int r) {
	if (r - l < 2) {
		return 0;
	}

	if (l == 0) {
		return 2 * r - 1 + solve(1, r);
	}

	ll res = 0;
	if (l & 1) {
		res += 2 * (get(l, r) - get(l, l));
		++l;
	}

	if (r & 1) {
		res += 2 * (get(r - 1, r) - get(r - 1, l));
		--r;
	}

	return res + 3 * solve(l >> 1, r >> 1);
}

int t;

int main() {
	cin >> t;

	int l, r;
	while (t--) {
		cin >> l >> r;
		cout << solve(l, r + 1) << endl;
	}
	//pause;
	return 0;
}


#include <bits/stdc++.h>

#pragma GCC optimize("O3")
using namespace std;
#define ll long long
#define ull unsigned long long
#define rep(i,n,N) for(ll i=n;i<=N;++i)
#define rap(i,n,N) for(ll i=n;i>=N;--i)
#define mp make_pair
#define pb push_back
#define pob pop_back
#define pf push_front
#define pof pop_front
#define fi first
#define se second
//#define ff fi.fi
//#define fs fi.se
#define sf se.fi
#define ss se.se
#define lc (id<<1)
#define rc ((id<<1)|1)
#define db(x) cerr << ">>>>>> " << #x << " -> " << x << endl
#define nani(x) cerr <<  "Line " << __LINE__ << ": " << #x << " is " << x << endl
#define all(x) x.begin(),x.end()
#define pii pair<int,int>
#define pll pair<ll,ll>
#define plll pair<ll,pll>
#define piii pair<int,pii>
#define piiii pair<pii,pii>
#define psi pair<string,int>
#define endl '\n'
const int MAX = 20+5;
const ll MAX2 = 11;
const ll MOD = 1000000007;
const ll MOD2 = 1000005329;
const ll INF = 2e18;
const int dr[]={1,0,-1,0,1,1,-1,-1,0};
const int dc[]={0,1,0,-1,1,-1,1,-1,0};
const double pi = acos(-1);
const double EPS = 1e-9;
const int block = 2000;

ll tc,z,l,r,dp[30][2][2][2][2],ans;

ll f(int pos,bool x,bool y,bool a,bool b){
	if(pos<0)return 1;
	ll &ret = dp[pos][x][y][a][b];
	if(ret!=-1)return ret;
	ret = 0;
	if(r&(1<<pos)){
		if(l&(1<<pos)){
			//0 0
			if(a&b)ret+= f(pos-1,1,1,a,b);
			//1 0
			if(b)ret+= f(pos-1,x,1,a,b);
			//0 1
			if(a)ret+= f(pos-1,1,y,a,b);
		}
		else {
			//0 0
			ret+= f(pos-1,1,1,a,b);
			//1 0
			ret+= f(pos-1,x,1,1,b);
			//0 1
			ret+= f(pos-1,1,y,a,1);
		}
	}
	else {
		if(l&(1<<pos)){
			//0 0
			if(a&b)ret+= f(pos-1,x,y,a,b);
			//1 0
			if(b&x)ret+= f(pos-1,x,y,a,b);
			//0 1
			if(a&y)ret+= f(pos-1,x,y,a,b);
		}
		else {
			//0 0
			ret+= f(pos-1,x,y,a,b);
			//1 0
			if(x)ret+= f(pos-1,x,y,1,b);
			//0 1
			if(y)ret+= f(pos-1,x,y,a,1);
		}
	}
	return ret;
}

int main(){
//	cout<<fixed<<setprecision(10);
//    freopen("input.txt", "r", stdin);
//	freopen("output.txt","w",stdout);
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    cin>>tc;
    while(tc--){
    	cin>>l>>r;
    	memset(dp,-1,sizeof dp);
    	cout<<f(29,0,0,0,0)<<endl;
	}
    return 0;
}


#include <bits/stdc++.h>

#pragma GCC optimize("O3")
using namespace std;
#define ll long long
#define ull unsigned long long
#define rep(i,n,N) for(ll i=n;i<=N;++i)
#define rap(i,n,N) for(ll i=n;i>=N;--i)
#define mp make_pair
#define pb push_back
#define pob pop_back
#define pf push_front
#define pof pop_front
#define fi first
#define se second
//#define ff fi.fi
//#define fs fi.se
#define sf se.fi
#define ss se.se
#define lc (id<<1)
#define rc ((id<<1)|1)
#define db(x) cerr << ">>>>>> " << #x << " -> " << x << endl
#define nani(x) cerr <<  "Line " << __LINE__ << ": " << #x << " is " << x << endl
#define all(x) x.begin(),x.end()
#define pii pair<int,int>
#define pll pair<ll,ll>
#define plll pair<ll,pll>
#define piii pair<int,pii>
#define piiii pair<pii,pii>
#define psi pair<string,int>
#define endl '\n'
const int MAX = 20+5;
const ll MAX2 = 11;
const ll MOD = 1000000007;
const ll MOD2 = 1000005329;
const ll INF = 2e18;
const int dr[]={1,0,-1,0,1,1,-1,-1,0};
const int dc[]={0,1,0,-1,1,-1,1,-1,0};
const double pi = acos(-1);
const double EPS = 1e-9;
const int block = 2000;

ll tc,z,l,r,dp[30][2][2][2][2],ans;

ll f(int pos,bool x,bool y,bool a,bool b){
	if(pos<0)return 1;
	ll &ret = dp[pos][x][y][a][b];
	if(ret!=-1)return ret;
	ret = 0;
	if(r&(1<<pos)){
		if(l&(1<<pos)){
			//0 0
			if(a&b)ret+= f(pos-1,1,1,a,b);
			//1 0
			if(b)ret+= f(pos-1,1,1,a,b);
			//0 1
			if(a)ret+= f(pos-1,1,1,a,b);
		}
		else {
			//0 0
			ret+= f(pos-1,1,1,a,b);
			//1 0
			ret+= f(pos-1,x,1,1,b);
			//0 1
			ret+= f(pos-1,1,y,a,1);
		}
	}
	else {
		if(l&(1<<pos)){
			//0 0
			if(a&b)ret+= f(pos-1,x,y,a,b);
			//1 0
			if(b&x)ret+= f(pos-1,x,y,a,b);
			//0 1
			if(a&y)ret+= f(pos-1,x,y,a,b);
		}
		else {
			//0 0
			ret+= f(pos-1,x,y,a,b);
			//1 0
			if(x)ret+= f(pos-1,x,y,1,b);
			//0 1
			if(y)ret+= f(pos-1,x,y,a,1);
		}
	}
	return ret;
}

int main(){
//	cout<<fixed<<setprecision(10);
//    freopen("input.txt", "r", stdin);
//	freopen("output.txt","w",stdout);
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    cin>>tc;
    while(tc--){
    	cin>>l>>r;
    	memset(dp,-1,sizeof dp);
    	cout<<f(29,0,0,0,0)<<endl;
	}
    return 0;
}


#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
using namespace std;
typedef long long LL;
int T,a[70],b[70];
LL f[70][2][2];

LL dfs(int pos,int lim1,int lim2){
	if(pos==-1) return 1;
	if(f[pos][lim1][lim2]!=-1) return f[pos][lim1][lim2];
	LL temp=0;
	int up1=lim1?a[pos]:1,up2=lim2?b[pos]:1;
	for(int i=0;i<=up1;i++)
		for(int j=0;j<=up2;j++){
			if(i==1&&j==1) continue;
			temp+=dfs(pos-1,lim1&&i==a[pos],lim2&&j==b[pos]);
		}
	f[pos][lim1][lim2]=temp;
	return temp;
}

LL solve(int x,int y){
	memset(f,-1,sizeof(f));
	int pos;
	for(pos=0;x||y;pos++,x/=2,y/=2)
		a[pos]=x%2,b[pos]=y%2;
	return dfs(pos-1,1,1);
}

int main(){
	scanf("%d",&T);
	int l,r;
	memset(f,-1,sizeof(f));
	while(T--){
		scanf("%d%d",&l,&r);
		printf("%lld\n",solve(r,r)-2*solve(l-1,r)+solve(l-1,l-1));
	}
	return 0;
}


#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
using namespace std;
typedef long long LL;
int T,a[70],b[70];
LL f[70][2][2];

LL dfs(int pos,int pa,int pb,int lim1,int lim2){
	if(pos==-1) return 1;
	if(!lim1&&!lim2&&f[pos][pa][pb]!=-1) return f[pos][pa][pb];
	LL temp=0;
	int up1=lim1?a[pos]:1,up2=lim2?b[pos]:1;
	for(int i=0;i<=up1;i++)
		for(int j=0;j<=up2;j++){
			if(i==1&&j==1) continue;
			temp+=dfs(pos-1,i,j,lim1&&i==a[pos],lim2&&j==b[pos]);
		}
	if(!lim1&&!lim2) f[pos][pa][pb]=temp;
	return temp;
}

LL solve(int x,int y){
	int pos;
	for(pos=0;x||y;pos++,x/=2,y/=2)
		a[pos]=x%2,b[pos]=y%2;
	return dfs(pos-1,0,0,1,1);
}

int main(){
	scanf("%d",&T);
	int l,r;
	memset(f,-1,sizeof(f));
	while(T--){
		scanf("%d%d",&l,&r);
		printf("%lld\n",solve(r,r)-2*solve(l-1,r)+solve(l-1,l-1));
	}
	return 0;
}


#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
using namespace std;
typedef long long LL;
int T,a[70],b[70];
LL f[70][2][2];

LL dfs(int pos,int pa,int pb,int lim1,int lim2){
	if(pos==-1) return 1;
	if(!lim1&&!lim2&&f[pos][pa][pb]!=-1) return f[pos][pa][pb];
	LL temp=0;
	int up1=lim1?a[pos]:1,up2=lim2?b[pos]:1;
	for(int i=0;i<=up1;i++)
		for(int j=0;j<=up2;j++){
			if(i==1&&j==1) continue;
			temp+=dfs(pos-1,i,j,lim1&&i==a[pos],lim2&&j==b[pos]);
		}
	if(!lim1&&!lim2) f[pos][pa][pb]=temp;
	return temp;
}

LL solve(int x,int y){
	int pos;
	for(pos=0;x||y;pos++,x/=2,y/=2)
		a[pos]=x%2,b[pos]=y%2;
	return dfs(pos-1,0,0,1,1);
}

int main(){
	scanf("%d",&T);
	int l,r;
	while(T--){
		memset(f,-1,sizeof(f));
		scanf("%d%d",&l,&r);
		printf("%lld\n",solve(r,r)-2*solve(l-1,r)+solve(l-1,l-1));
	}
	return 0;
}


#ifdef _LOCAL
#	include <dbg_print.h>
#else
#	define NDEBUG
#	define dprint(...)
#endif

#include <cmath>
#include <cstdio>
#include <cstring>
#include <vector>
#include <iostream>
#include <algorithm>
#include <assert.h>
using namespace std;
typedef long long ll;

int main()
{
	ios::sync_with_stdio(false);

	int t;
	cin >> t;
	while(t--)
	{
		int l, r;
		cin >> l >> r;
		ll d[2][2][2][64] = {0}; // [r==a][b==l][b==a]

		d[1][1][1][32] = 1;
		for(int i=31; 0<=i; --i)
		{
			int rbit = (r>>i)&1;
			int lbit = (l>>i)&1;
			for(int x1=0; x1<2; ++x1)
				for(int y1=0; y1<2; ++y1)
					for(int z1=0; z1<2; ++z1)
						for(int abit=0; abit<2; ++abit)
							for(int bbit=0; bbit<2; ++bbit) if(!(abit&bbit))
							{
								int x2 = x1 & (abit == rbit ? 1 : 0);
								int y2 = y1 & (bbit == lbit ? 1 : 0);
								int z2 = z1 & (abit == bbit);
								if(x1 && rbit < abit)
									continue;
								if(y1 && bbit < lbit)
									continue;
								if(z1 && abit < bbit)
									continue;

								d[x2][y2][z2][i] += d[x1][y1][z1][i+1];
							}
		}
		ll res = 0;
		for(int x1=0; x1<2; ++x1)
			for(int y1=0; y1<2; ++y1)
				for(int z1=0; z1<2; ++z1)
					res += d[x1][y1][z1][0];
		cout << 2*res << "\n";
	}

	return 0;
}


#ifdef _LOCAL
#	include <dbg_print.h>
#else
#	define NDEBUG
#	define dprint(...)
#endif

#include <cmath>
#include <cstdio>
#include <cstring>
#include <vector>
#include <iostream>
#include <algorithm>
#include <assert.h>
using namespace std;
typedef long long ll;

int main()
{
	ios::sync_with_stdio(false);

	int t;
	cin >> t;
	while(t--)
	{
		int l, r;
		cin >> l >> r;
		ll d[2][2][2][64] = {0}; // [r==a][b==l][b==a]

		d[1][1][1][32] = 1;
		for(int i=31; 0<=i; --i)
		{
			int rbit = (r>>i)&1;
			int lbit = (l>>i)&1;
			for(int x1=0; x1<2; ++x1)
				for(int y1=0; y1<2; ++y1)
					for(int z1=0; z1<2; ++z1)
						for(int abit=0; abit<2; ++abit)
							for(int bbit=0; bbit<2; ++bbit) if(!(abit&bbit))
							{
								int x2 = x1 & (abit == rbit ? 1 : 0);
								int y2 = y1 & (bbit == lbit ? 1 : 0);
								int z2 = z1 & (abit == bbit);
								if(x1 && rbit < abit)
									continue;
								if(y1 && bbit < lbit)
									continue;
								if(z1 && abit < bbit)
									continue;

								d[x2][y2][z2][i] += d[x1][y1][z1][i+1];
							}
		}
		ll res = 0;
		for(int x1=0; x1<2; ++x1)
			for(int y1=0; y1<2; ++y1)
				for(int z1=0; z1<2; ++z1)
					res += d[x1][y1][z1][0];
		res *= 2;
		if(l == 0)
			res -= 1;
		cout << res << "\n";
	}

	return 0;
}


#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-l+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();
}


#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*2+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();
}