import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); StringBuilder sb = new StringBuilder(); for (int i = 0; i < n; i++) { int a = sc.nextInt(); long b = sc.nextInt(); int c = sc.nextInt(); if (c == 1) { sb.append((long)c * b).append("\n"); continue; } long total = 0; while (a >= 2 * c) { if (a % c != 0) { total++; a = a / c * c; } else { a /= Math.min(c, a / c); total++; } } if (a >= c) { total += 2; } else { total++; } sb.append(total * b).append("\n"); } System.out.println(sb); } }