#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */

/* Variables and functions */
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ scanf(char *, int *, ...);
int main() {
  int t, n, c, m, i, j, count = 0, rem = 0, d;
  scanf("%d", &t);
  for (i = 0; i < t; i++) {
    scanf("%d %d %d", &n, &c, &m);
    count = n / c;
    j = n / c;
    while (j > 0) {
      d = j;
      j = (j + rem) / m;
      rem = (d + rem) % m;
      count += j;
    }
    printf("%d\n", count);
    count = 0;
    d = 0;
    rem = 0;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
lea    0xec2(%rip),%rdi
lea    -0x8(%rbp),%rsi
mov    $0x0,%al
call   1040 <scanf@plt>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    11fa <main+0xda>
lea    0xe97(%rip),%rdi
lea    -0xc(%rbp),%rsi
lea    -0x10(%rbp),%rdx
lea    -0x14(%rbp),%rcx
mov    $0x0,%al
call   1040 <scanf@plt>
mov    -0xc(%rbp),%eax
cltd
idivl  -0x10(%rbp)
mov    %eax,-0x20(%rbp)
mov    -0xc(%rbp),%eax
cltd
idivl  -0x10(%rbp)
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jle    11c8 <main+0xa8>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x1c(%rbp),%eax
add    -0x24(%rbp),%eax
cltd
idivl  -0x14(%rbp)
mov    %eax,-0x1c(%rbp)
mov    -0x28(%rbp),%eax
add    -0x24(%rbp),%eax
cltd
idivl  -0x14(%rbp)
mov    %edx,-0x24(%rbp)
mov    -0x1c(%rbp),%eax
add    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
jmp    1190 <main+0x70>
mov    -0x20(%rbp),%esi
lea    0xe37(%rip),%rdi
call   1030 <printf@plt>
movl   $0x0,-0x20(%rbp)
movl   $0x0,-0x28(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1156 <main+0x36>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
lea    0xed1(%rip),%rdi
lea    0x8(%rsp),%rsi
xor    %eax,%eax
call   1040 <scanf@plt>
cmpl   $0x0,0x8(%rsp)
jle    11bf <main+0x9f>
lea    0xeb1(%rip),%r14
lea    0x14(%rsp),%r15
lea    0x10(%rsp),%r12
lea    0xc(%rsp),%r13
lea    0xea4(%rip),%rbx
xor    %ebp,%ebp
jmp    1181 <main+0x61>
nopl   0x0(%rax)
mov    %rbx,%rdi
call   1030 <printf@plt>
add    $0x1,%ebp
cmp    0x8(%rsp),%ebp
jge    11bf <main+0x9f>
mov    %r14,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
mov    %r13,%rcx
xor    %eax,%eax
call   1040 <scanf@plt>
mov    0x14(%rsp),%eax
cltd
idivl  0x10(%rsp)
mov    %eax,%esi
test   %eax,%eax
jle    1170 <main+0x50>
xor    %edx,%edx
mov    0xc(%rsp),%ecx
mov    %esi,%eax
nopl   0x0(%rax,%rax,1)
add    %eax,%edx
mov    %edx,%eax
cltd
idiv   %ecx
add    %eax,%esi
test   %eax,%eax
jg     11b0 <main+0x90>
jmp    1170 <main+0x50>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%d\00"
@.str.1 = [9 x i8] c"%d %d %d\00"
@.str.2 = [4 x i8] c"%d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 0, i32* %1
store i32 0, i32* %8
store i32 0, i32* %9
%11 = call i32 (i8*, i32*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %2)
store i32 0, i32* %6
br label %12
12: ; preds = %45, %0
%13 = load i32, i32* %6
%14 = load i32, i32* %2
%15 = icmp slt i32 %13, %14
br i1 %15, label %16, label %48
16: ; preds = %12
%17 = call i32 (i8*, i32*, ...) @scanf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32* %3, i32* %4, i32* %5)
%18 = load i32, i32* %3
%19 = load i32, i32* %4
%20 = sdiv i32 %18, %19
store i32 %20, i32* %8
%21 = load i32, i32* %3
%22 = load i32, i32* %4
%23 = sdiv i32 %21, %22
store i32 %23, i32* %7
br label %24
24: ; preds = %27, %16
%25 = load i32, i32* %7
%26 = icmp sgt i32 %25, 0
br i1 %26, label %27, label %42
27: ; preds = %24
%28 = load i32, i32* %7
store i32 %28, i32* %10
%29 = load i32, i32* %7
%30 = load i32, i32* %9
%31 = add nsw i32 %29, %30
%32 = load i32, i32* %5
%33 = sdiv i32 %31, %32
store i32 %33, i32* %7
%34 = load i32, i32* %10
%35 = load i32, i32* %9
%36 = add nsw i32 %34, %35
%37 = load i32, i32* %5
%38 = srem i32 %36, %37
store i32 %38, i32* %9
%39 = load i32, i32* %7
%40 = load i32, i32* %8
%41 = add nsw i32 %40, %39
store i32 %41, i32* %8
br label %24
42: ; preds = %24
%43 = load i32, i32* %8
%44 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %43)
store i32 0, i32* %8
store i32 0, i32* %10
store i32 0, i32* %9
br label %45
45: ; preds = %42
%46 = load i32, i32* %6
%47 = add nsw i32 %46, 1
store i32 %47, i32* %6
br label %12
48: ; preds = %12
ret i32 0
}

LLVM IR (-O2):
@.str = [3 x i8] c"%d\00"
@.str.1 = [9 x i8] c"%d %d %d\00"
@.str.2 = [4 x i8] c"%d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = bitcast i32* %1 to i8*
%6 = bitcast i32* %2 to i8*
%7 = bitcast i32* %3 to i8*
%8 = bitcast i32* %4 to i8*
%9 = call i32 (i8*, i32*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %1)
%10 = load i32, i32* %1
%11 = icmp sgt i32 %10, 0
br i1 %11, label %12, label %36
12: ; preds = %0, %30
%13 = phi i32 [ %33, %30 ], [ 0, %0 ]
%14 = call i32 (i8*, i32*, ...) @scanf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32* %2, i32* %3, i32* %4)
%15 = load i32, i32* %2
%16 = load i32, i32* %3
%17 = sdiv i32 %15, %16
%18 = icmp sgt i32 %17, 0
br i1 %18, label %19, label %30
19: ; preds = %12
%20 = load i32, i32* %4
br label %21
21: ; preds = %19, %21
%22 = phi i32 [ 0, %19 ], [ %27, %21 ]
%23 = phi i32 [ %17, %19 ], [ %28, %21 ]
%24 = phi i32 [ %17, %19 ], [ %26, %21 ]
%25 = add nsw i32 %22, %24
%26 = sdiv i32 %25, %20
%27 = srem i32 %25, %20
%28 = add nsw i32 %26, %23
%29 = icmp sgt i32 %26, 0
br i1 %29, label %21, label %30
30: ; preds = %21, %12
%31 = phi i32 [ %17, %12 ], [ %28, %21 ]
%32 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %31)
%33 = add nuw nsw i32 %13, 1
%34 = load i32, i32* %1
%35 = icmp slt i32 %33, %34
br i1 %35, label %12, label %36
36: ; preds = %30, %0
ret i32 0
}