While doing some spring cleaning, Daniel found an old calculator that he loves so much. However, it seems like it is broken. When he tries to compute $1 + 3$ using the calculator, he gets $2$ instead of $4$. But when he tries computing $1 + 4$, he gets the correct answer, $5$. Puzzled by this mystery, he opened up his calculator and found the answer to the riddle: the full adders became half adders! 

So, when he tries to compute the sum $a + b$ using the calculator, he instead gets the xorsum $a \oplus b$ (read the definition by the link: https://en.wikipedia.org/wiki/Exclusive_or).

As he saw earlier, the calculator sometimes gives the correct answer. And so, he wonders, given integers $l$ and $r$, how many pairs of integers $(a, b)$ satisfy the following conditions: $$a + b = a \oplus b$$ $$l \leq a \leq r$$ $$l \leq b \leq r$$

However, Daniel the Barman is going to the bar and will return in two hours. He tells you to solve the problem before he returns, or else you will have to enjoy being blocked.


-----Input-----

The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of testcases.

Then, $t$ lines follow, each containing two space-separated integers $l$ and $r$ ($0 \le l \le r \le 10^9$).


-----Output-----

Print $t$ integers, the $i$-th integer should be the answer to the $i$-th testcase.


-----Example-----
Input
3
1 4
323 323
1 1000000

Output
8
0
3439863766



-----Note-----

$a \oplus b$ denotes the bitwise XOR of $a$ and $b$.

For the first testcase, the pairs are: $(1, 2)$, $(1, 4)$, $(2, 1)$, $(2, 4)$, $(3, 4)$, $(4, 1)$, $(4, 2)$, and $(4, 3)$.

Difficulty: HARD
Tags: Dynamic programming

Bit manipulation

Combinatorics

Complete search
Skill types: Dynamic programming

Bit manipulation

Complete search

Starter code:

Solutions:
def solve(L, R):
	res = 0
	for i in range(32):
		for j in range(32):
			l = L >> i << i
			r = R >> j << j
			if l >> i & 1 == 0 or r >> j & 1 == 0:
				continue
			l -= 1 << i
			r -= 1 << j
			if l & r:
				continue
			lr = l ^ r
			ma = max(i, j)
			mi = min(i, j)
			mask = (1 << ma) - 1
			p = bin(lr & mask).count('1')
			ip = ma - mi - p
			res += 3 ** mi * 2 ** ip
	return res
T = int(input())
for _ in range(T):
	(l, r) = list(map(int, input().split()))
	print(solve(r + 1, r + 1) + solve(l, l) - solve(l, r + 1) * 2)


def g(a, b):
	cur = 1
	res = 0
	ze = 0
	while cur <= b:
		if b & cur:
			b ^= cur
			if a & b == 0:
				res += 1 << ze
		if a & cur == 0:
			ze = ze + 1
		cur <<= 1
	return res

def f(a, b):
	res = 0
	if a == b:
		return 0
	if a == 0:
		return 2 * b - 1 + f(1, b)
	if a & 1:
		res = res + 2 * (g(a, b) - g(a, a))
		a = a + 1
	if b & 1:
		res = res + 2 * (g(b - 1, b) - g(b - 1, a))
	return 3 * f(a >> 1, b >> 1) + res
t = int(input())
while t > 0:
	t = t - 1
	(l, r) = map(int, input().split())
	print(f(l, r + 1))


def get_bin(a):
	nums = []
	for i in range(32):
		if 1 << i & a:
			nums.append(1)
		else:
			nums.append(0)
	while len(nums) > 0 and nums[-1] == 0:
		nums.pop()
	return nums
dp = {}

def get_num(a, b):
	nonlocal dp
	if (a, b) in dp:
		return dp[a, b]
	if a < 0 or b < 0:
		return 0
	if a == 0 and b == 0:
		return 1
	a_bin = get_bin(a)
	b_bin = get_bin(b)
	if b > a:
		(a_bin, b_bin) = (b_bin, a_bin)
		(a, b) = (b, a)
	if len(a_bin) > len(b_bin):
		big_bit = 1 << len(a_bin) - 1
		to_ret = get_num(big_bit - 1, b) + get_num(a - big_bit, b)
		dp[a, b] = to_ret
		return to_ret
	if sum(a_bin) == len(a_bin) and sum(b_bin) == len(b_bin):
		to_ret = pow(3, len(a_bin))
		dp[a, b] = to_ret
		return to_ret
	big_bit = 1 << len(a_bin) - 1
	to_ret = get_num(big_bit - 1, b - big_bit) + get_num(a, big_bit - 1)
	dp[a, b] = to_ret
	return to_ret
tc = int(input(''))
for i in range(int(tc)):
	nums = input('').split(' ')
	l = int(nums[0])
	r = int(nums[1])
	ans = get_num(r, r) - 2 * get_num(r, l - 1) + get_num(l - 1, l - 1)
	print(ans)