use std::io::Read; fn main() { let mut all_data = String::new(); std::io::stdin().read_to_string(&mut all_data).ok(); let mut all_data = all_data.trim().split('\n').map(|s| s.trim()); let q: usize = all_data.next().unwrap().parse().unwrap(); all_data.take(q).map(|s| s.split_whitespace().map(|i| i.parse().unwrap()).collect::>()).for_each(|abc| { let mut a = abc[0]; let b = abc[1]; let c = abc[2]; if c == 1 { println!("{}", -1); } else { let mut count = 0; while a > 2*c - 2 { count += if a % c == 0 { 0 } else { 1 }; a /= c; count += 1; } if a < c { count += 1; } else { count += 2; } println!("{}", b * count); } }); }