#[allow(unused_macros)] macro_rules! read_to_tuple { ( $( $t:ty ),* ) => {{ let mut input = String::new(); std::io::stdin().read_line(&mut input).unwrap(); let mut iter = input.split_whitespace(); ( $( iter.next().unwrap().parse::<$t>().unwrap() ),* ) }} } fn f(d: i64, mut n: i64) -> i64 { let mut res = 0; while n != 0 { res += n % d; n /= d; } res } fn ff(d: i64, mut n: i64) -> i64 { let mut m = f(d, n); while m != n { n = m; m = f(d, n); } n } fn solve() -> String { let (d, a, b) = read_to_tuple!(i64, i64, i64); let mut c = b - a + 1; let n = ff(d, a); let ans = if c <= d - n { (n + (n + c - 1)) * c / 2 } else { let mut res = (n + (d - 1)) * (d - n) / 2; c -= d - n; res += (1 + (d - 1)) * (d - 1) / 2 * (c / (d - 1)); c %= d - 1; res += (1 + c) * c / 2; res }; ans.to_string() } fn main() { let t = read_to_tuple!(usize); let answers = (0..t).map(|_| solve()).collect::>(); println!("{}", answers.join("\n")); }