#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, i, e, f, c;
  int count;
  scanf("%d", &t);
  for (i = 0; i < t; i++) {
    scanf("%d %d %d", &e, &f, &c);
    count = 0;
    e = e + f;
    while (e >= c) {
      count += e / c;
      e = (e / c) + (e % c);
    }
    printf("%d\n", count);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xed0(%rip),%rdi
lea    -0x8(%rbp),%rsi
mov    $0x0,%al
call   1040 <scanf@plt>
movl   $0x0,-0xc(%rbp)
mov    -0xc(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    11d2 <main+0xb2>
lea    0xea5(%rip),%rdi
lea    -0x10(%rbp),%rsi
lea    -0x14(%rbp),%rdx
lea    -0x18(%rbp),%rcx
mov    $0x0,%al
call   1040 <scanf@plt>
movl   $0x0,-0x1c(%rbp)
mov    -0x10(%rbp),%eax
add    -0x14(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0x18(%rbp),%eax
jl     11b5 <main+0x95>
mov    -0x10(%rbp),%eax
cltd
idivl  -0x18(%rbp)
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%eax
cltd
idivl  -0x18(%rbp)
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%eax
cltd
idivl  -0x18(%rbp)
mov    -0x20(%rbp),%eax
add    %edx,%eax
mov    %eax,-0x10(%rbp)
jmp    117e <main+0x5e>
mov    -0x1c(%rbp),%esi
lea    0xe4a(%rip),%rdi
call   1030 <printf@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1148 <main+0x28>
xor    %eax,%eax
add    $0x20,%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
xor    %r14d,%r14d
lea    0xc(%rsp),%rsi
xor    %eax,%eax
call   1040 <scanf@plt>
cmpl   $0x0,0xc(%rsp)
jle    11cf <main+0xaf>
lea    0x8(%rsp),%r15
lea    0x14(%rsp),%r12
lea    0x10(%rsp),%r13
lea    0xea4(%rip),%rbx
jmp    1189 <main+0x69>
nopw   0x0(%rax,%rax,1)
mov    %edx,0x8(%rsp)
mov    %rbx,%rdi
mov    %ebp,%esi
call   1030 <printf@plt>
add    $0x1,%r14d
cmp    0xc(%rsp),%r14d
jge    11cf <main+0xaf>
xor    %ebp,%ebp
lea    0xe6e(%rip),%rdi
mov    %r15,%rsi
mov    %r12,%rdx
mov    %r13,%rcx
xor    %eax,%eax
call   1040 <scanf@plt>
mov    0x14(%rsp),%edx
add    0x8(%rsp),%edx
mov    0x10(%rsp),%ecx
cmp    %ecx,%edx
jl     1170 <main+0x50>
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %edx,%eax
cltd
idiv   %ecx
add    %eax,%ebp
add    %eax,%edx
cmp    %ecx,%edx
jge    11c0 <main+0xa0>
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
store i32 0, i32* %1
%8 = 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* %3
br label %9
9: ; preds = %38, %0
%10 = load i32, i32* %3
%11 = load i32, i32* %2
%12 = icmp slt i32 %10, %11
br i1 %12, label %13, label %41
13: ; preds = %9
%14 = call i32 (i8*, i32*, ...) @scanf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32* %4, i32* %5, i32* %6)
store i32 0, i32* %7
%15 = load i32, i32* %4
%16 = load i32, i32* %5
%17 = add nsw i32 %15, %16
store i32 %17, i32* %4
br label %18
18: ; preds = %22, %13
%19 = load i32, i32* %4
%20 = load i32, i32* %6
%21 = icmp sge i32 %19, %20
br i1 %21, label %22, label %35
22: ; preds = %18
%23 = load i32, i32* %4
%24 = load i32, i32* %6
%25 = sdiv i32 %23, %24
%26 = load i32, i32* %7
%27 = add nsw i32 %26, %25
store i32 %27, i32* %7
%28 = load i32, i32* %4
%29 = load i32, i32* %6
%30 = sdiv i32 %28, %29
%31 = load i32, i32* %4
%32 = load i32, i32* %6
%33 = srem i32 %31, %32
%34 = add nsw i32 %30, %33
store i32 %34, i32* %4
br label %18
35: ; preds = %18
%36 = load i32, i32* %7
%37 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %36)
br label %38
38: ; preds = %35
%39 = load i32, i32* %3
%40 = add nsw i32 %39, 1
store i32 %40, i32* %3
br label %9
41: ; preds = %9
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 %35
12: ; preds = %0, %28
%13 = phi i32 [ %32, %28 ], [ 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 = add nsw i32 %16, %15
%18 = load i32, i32* %4
%19 = icmp slt i32 %17, %18
br i1 %19, label %28, label %20
20: ; preds = %12, %20
%21 = phi i32 [ %24, %20 ], [ 0, %12 ]
%22 = phi i32 [ %26, %20 ], [ %17, %12 ]
%23 = sdiv i32 %22, %18
%24 = add nsw i32 %23, %21
%25 = srem i32 %22, %18
%26 = add nsw i32 %25, %23
%27 = icmp slt i32 %26, %18
br i1 %27, label %28, label %20
28: ; preds = %20, %12
%29 = phi i32 [ %17, %12 ], [ %26, %20 ]
%30 = phi i32 [ 0, %12 ], [ %24, %20 ]
store i32 %29, i32* %2
%31 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %30)
%32 = add nuw nsw i32 %13, 1
%33 = load i32, i32* %1
%34 = icmp slt i32 %32, %33
br i1 %34, label %12, label %35
35: ; preds = %28, %0
ret i32 0
}