← Home
```go
package main

import (
	"bufio"
	"os"
	"strconv"
)

func main() {
	// Use buffered I/O for speed
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	// Read number of test cases
	if !scanner.Scan() {
		return
	}
	t, _ := strconv.Atoi(scanner.Text())

	for i := 0; i < t; i++ {
		if !scanner.Scan() {
			break
		}
		n, _ := strconv.ParseInt(scanner.Text(), 10, 64)
		if !scanner.Scan() {
			break
		}
		k, _ := strconv.ParseInt(scanner.Text(), 10, 64)

		a, b := solve(n, k)
		// The total lucky value for segment [1, n] is A*1 + B
		ans := a + b
		writer.WriteString(strconv.FormatInt(ans, 10))
		writer.WriteByte('\n')
	}
}

// solve returns coefficients (A, B) such that the lucky value sum
// for a segment of length 'len' starting at index 'l' is A*l + B.
func solve(len, k int64) (int64, int64) {
	if len < k {
		return 0, 0
	}

	h := len / 2
	a, b := solve(h, k)

	if len%2 == 0 {
		// Case: length is even
		// The segment [l, r] is split into [l, m] and [m+1, r], both of length h.
		// Sum = (A*l + B) + (A*(l+h) + B)
		//     = 2*A*l + 2*B + A*h
		// New A = 2*A
		// New B = 2*B + A*h
		return 2 * a, 2*b + a*h
	} else {
		// Case: length is odd
		// Midpoint m = l + h is added to the lucky value.
		// Then split into [l, m-1] and [m+1, r], both of length h.
		// Sum = (l+h) + (A*l + B) + (A*(l+h+1) + B)
		//     = l + h + A*l + B + A*l + A*h + A + B
		//     = l*(1 + 2*A) + (h + 2*B + A*h + A)
		// New A = 2*A + 1
		// New B = 2*B + h + A*(h+1)
		return 2*a + 1, 2*b + h + a*(h+1)
	}
}
```