← Home
```go
package main

import (
	"bufio"
	"fmt"
	"os"
)

var memo [32][2][2]int64
var numA, numB int

func solve(idx int, tightA, tightB bool) int64 {
	if idx < 0 {
		return 1
	}
	tA := 0
	if tightA {
		tA = 1
	}
	tB := 0
	if tightB {
		tB = 1
	}
	if memo[idx][tA][tB] != -1 {
		return memo[idx][tA][tB]
	}

	var res int64 = 0
	limA := 1
	if tightA {
		limA = (numA >> idx) & 1
	}
	limB := 1
	if tightB {
		limB = (numB >> idx) & 1
	}

	for a := 0; a <= limA; a++ {
		for b := 0; b <= limB; b++ {
			if a&b == 0 {
				newTightA := tightA && (a == limA)
				newTightB := tightB && (b == limB)
				res += solve(idx-1, newTightA, newTightB)
			}
		}
	}

	memo[idx][tA][tB] = res
	return res
}

func calc(n, m int) int64 {
	if n < 0 || m < 0 {
		return 0
	}
	numA = n
	numB = m
	for i := 0; i < 32; i++ {
		for j := 0; j < 2; j++ {
			for k := 0; k < 2; k++ {
				memo[i][j][k] = -1
			}
		}
	}
	// 10^9 fits in roughly 30 bits, start from 30 down to 0
	return solve(30, true, true)
}

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()

	var t int
	fmt.Fscan(in, &t)

	for i := 0; i < t; i++ {
		var l, r int
		fmt.Fscan(in, &l, &r)
		// Principle of Inclusion-Exclusion on 2D range
		// Count(l...r, l...r) = Count(0...r, 0...r) - 2*Count(0...l-1, 0...r) + Count(0...l-1, 0...l-1)
		ans := calc(r, r) - 2*calc(l-1, r) + calc(l-1, l-1)
		fmt.Fprintln(out, ans)
	}
}
```