func main() var t: int :: cui@inputInt() for(1, t) var d: int :: cui@inputInt() var a: int :: cui@inputInt() var b: int :: cui@inputInt() var ans: int :: 0 if(a = b) do ans :: f(a, d) else do ans :+ (b - a + 1) / (d - 1) * d * (d - 1) / 2 var s: int :: f(a, d) var e: int :: f(b, d) if(s = e) if(d <> 2) do ans :+ s end if elif(s < e) do ans :+ sig(s, e) else do ans :+ sig(s, d - 1) + sig(1, e) end if func sig(s: int, e: int): int ret(e * e + e - (s - 1) * (s - 1) - (s - 1)) / 2 end func end if if(a = 0) do ans :- d - 1 end if do cui@print("\{ans}\n") func f(a: int, d: int): int ret 1 + (a + d - 2) % (d - 1) end func end for end func