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 = 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 print(ans + head + tail)