t = int(input()) def digit_sum(x, d): while x >= d: tmp = 0 while x > 0: tmp += x % d x //= d x = tmp return x for _ in range(t): d, a, b = map(int, input().split()) left = digit_sum(a, d) right = digit_sum(b, d) # one_cycle = (d - 1) * d // 2 # cycle_len = d - 1 n = b - a + 1 # ds = digit_sum(a, d) ans = 0 # ans = n // cycle_len * one_cycle # head = one_cycle - (left - 1) * left // 2 if left != 1 else 0 # tail = right * (right + 1) // 2 if right < d - 1 else 0 for i in range(n): ans += left left += 1 if left == d: left = 1 #print(ans + head + tail) print(ans)