#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*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ scanf(char *, int *, ...);
int /*<<< orphan*/ stdin;
int main(int argc, char *argv[]) {
  int n, have, collection, change, all, drink;
  fflush(stdin);
  scanf("%d", &n);
  while (n--) {
    all = 0;
    scanf("%d %d %d", &have, &collection, &change);
    have += collection;
    while (have >= change) {
      drink = have / change;
      all += drink;
      have = have % change + drink;
    }
    printf("%d\n", all);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e9b(%rip),%rax
mov    (%rax),%edi
call   1040 <fflush@plt>
lea    0xeab(%rip),%rdi
lea    -0x14(%rbp),%rsi
mov    $0x0,%al
call   1050 <scanf@plt>
mov    -0x14(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x14(%rbp)
cmp    $0x0,%eax
je     11e9 <main+0xb9>
movl   $0x0,-0x24(%rbp)
lea    0xe78(%rip),%rdi
lea    -0x18(%rbp),%rsi
lea    -0x1c(%rbp),%rdx
lea    -0x20(%rbp),%rcx
mov    $0x0,%al
call   1050 <scanf@plt>
mov    -0x1c(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x20(%rbp),%eax
jl     11d5 <main+0xa5>
mov    -0x18(%rbp),%eax
cltd
idivl  -0x20(%rbp)
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%eax
add    -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%eax
cltd
idivl  -0x20(%rbp)
add    -0x28(%rbp),%edx
mov    %edx,-0x18(%rbp)
jmp    11a4 <main+0x74>
mov    -0x24(%rbp),%esi
lea    0xe2a(%rip),%rdi
call   1030 <printf@plt>
jmp    1166 <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
mov    0x2ea3(%rip),%rax
mov    (%rax),%edi
call   1040 <fflush@plt>
lea    0xeb3(%rip),%rdi
lea    0xc(%rsp),%rsi
xor    %eax,%eax
call   1050 <scanf@plt>
subl   $0x1,0xc(%rsp)
jae    1177 <main+0x47>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe82(%rip),%r14
lea    0x8(%rsp),%r15
lea    0x14(%rsp),%r12
lea    0x10(%rsp),%r13
lea    0xe75(%rip),%rbx
jmp    11b5 <main+0x85>
cs nopw 0x0(%rax,%rax,1)
mov    %edx,0x8(%rsp)
mov    %rbx,%rdi
mov    %ebp,%esi
call   1030 <printf@plt>
subl   $0x1,0xc(%rsp)
jb     1166 <main+0x36>
xor    %ebp,%ebp
mov    %r14,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
mov    %r13,%rcx
xor    %eax,%eax
call   1050 <scanf@plt>
mov    0x8(%rsp),%edx
add    0x14(%rsp),%edx
mov    0x10(%rsp),%ecx
cmp    %ecx,%edx
jl     11a0 <main+0x70>
xor    %ebp,%ebp
nopl   0x0(%rax)
mov    %edx,%eax
cltd
idiv   %ecx
add    %eax,%ebp
add    %eax,%edx
cmp    %ecx,%edx
jge    11e0 <main+0xb0>
jmp    11a0 <main+0x70>

LLVM IR (-O0):
@stdin = global i32 0
@.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(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* @stdin
%13 = call i32 @fflush(i32 %12)
%14 = call i32 (i8*, i32*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %6)
br label %15
15: ; preds = %40, %2
%16 = load i32, i32* %6
%17 = add nsw i32 %16, -1
store i32 %17, i32* %6
%18 = icmp ne i32 %16, 0
br i1 %18, label %19, label %43
19: ; preds = %15
store i32 0, i32* %10
%20 = call i32 (i8*, i32*, ...) @scanf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32* %7, i32* %8, i32* %9)
%21 = load i32, i32* %8
%22 = load i32, i32* %7
%23 = add nsw i32 %22, %21
store i32 %23, i32* %7
br label %24
24: ; preds = %28, %19
%25 = load i32, i32* %7
%26 = load i32, i32* %9
%27 = icmp sge i32 %25, %26
br i1 %27, label %28, label %40
28: ; preds = %24
%29 = load i32, i32* %7
%30 = load i32, i32* %9
%31 = sdiv i32 %29, %30
store i32 %31, i32* %11
%32 = load i32, i32* %11
%33 = load i32, i32* %10
%34 = add nsw i32 %33, %32
store i32 %34, i32* %10
%35 = load i32, i32* %7
%36 = load i32, i32* %9
%37 = srem i32 %35, %36
%38 = load i32, i32* %11
%39 = add nsw i32 %37, %38
store i32 %39, i32* %7
br label %24
40: ; preds = %24
%41 = load i32, i32* %10
%42 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %41)
br label %15
43: ; preds = %15
ret i32 0
}

LLVM IR (-O2):
@stdin = global i32 0
@.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(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = bitcast i32* %3 to i8*
%8 = bitcast i32* %4 to i8*
%9 = bitcast i32* %5 to i8*
%10 = bitcast i32* %6 to i8*
%11 = load i32, i32* @stdin
%12 = tail call i32 @fflush(i32 %11)
%13 = call i32 (i8*, i32*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %3)
%14 = load i32, i32* %3
%15 = add nsw i32 %14, -1
store i32 %15, i32* %3
%16 = icmp eq i32 %14, 0
br i1 %16, label %39, label %17
17: ; preds = %2, %32
%18 = 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)
%19 = load i32, i32* %5
%20 = load i32, i32* %4
%21 = add nsw i32 %20, %19
%22 = load i32, i32* %6
%23 = icmp slt i32 %21, %22
br i1 %23, label %32, label %24
24: ; preds = %17, %24
%25 = phi i32 [ %28, %24 ], [ 0, %17 ]
%26 = phi i32 [ %30, %24 ], [ %21, %17 ]
%27 = sdiv i32 %26, %22
%28 = add nsw i32 %27, %25
%29 = srem i32 %26, %22
%30 = add nsw i32 %29, %27
%31 = icmp slt i32 %30, %22
br i1 %31, label %32, label %24
32: ; preds = %24, %17
%33 = phi i32 [ %21, %17 ], [ %30, %24 ]
%34 = phi i32 [ 0, %17 ], [ %28, %24 ]
store i32 %33, i32* %4
%35 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %34)
%36 = load i32, i32* %3
%37 = add nsw i32 %36, -1
store i32 %37, i32* %3
%38 = icmp eq i32 %36, 0
br i1 %38, label %39, label %17
39: ; preds = %32, %2
ret i32 0
}