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(-1).append("\n"); } else { sb.append(getCount(a, c) * b).append("\n"); } } System.out.print(sb); } static int getCount(int x, int y) { if (x == 0) { return 0; } else if (x % y == 0) { return getCount(x / y, y) + 1; } else if ((y - 1) * 2 >= x) { return 2; } else { return getCount(x - x % y, y) + 1; } } }