#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(void) {
  int T, numEmptyOwn, numEmptyFound, numToBuyNew, numDrinkTotal, numEmpty,
      numNewExchange;
  scanf("%d", &T);
  while (T) {
    scanf("%d %d %d", &numEmptyOwn, &numEmptyFound, &numToBuyNew);
    numDrinkTotal = 0;
    numEmpty = numEmptyOwn + numEmptyFound;
    while (numEmpty >= numToBuyNew) {
      numNewExchange = numEmpty / numToBuyNew;
      numEmpty = numEmpty % numToBuyNew;
      numDrinkTotal += numNewExchange;
      numEmpty += numNewExchange;
    }
    printf("%d\n", numDrinkTotal);
    T--;
  }
  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>
cmpl   $0x0,-0x8(%rbp)
je     11c9 <main+0xa9>
lea    0xeae(%rip),%rdi
lea    -0xc(%rbp),%rsi
lea    -0x10(%rbp),%rdx
lea    -0x14(%rbp),%rcx
mov    $0x0,%al
call   1040 <scanf@plt>
movl   $0x0,-0x18(%rbp)
mov    -0xc(%rbp),%eax
add    -0x10(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jl     11ac <main+0x8c>
mov    -0x1c(%rbp),%eax
cltd
idivl  -0x14(%rbp)
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%eax
cltd
idivl  -0x14(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x20(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x20(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
jmp    1175 <main+0x55>
mov    -0x18(%rbp),%esi
lea    0xe53(%rip),%rdi
call   1030 <printf@plt>
mov    -0x8(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x8(%rbp)
jmp    1141 <main+0x21>
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
lea    0x8(%rsp),%rsi
xor    %eax,%eax
call   1040 <scanf@plt>
cmpl   $0x0,0x8(%rsp)
je     11bf <main+0x9f>
lea    0xeb1(%rip),%r14
lea    0x14(%rsp),%r15
lea    0x10(%rsp),%r12
lea    0xc(%rsp),%r13
lea    0xea4(%rip),%rbx
jmp    1181 <main+0x61>
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %ebp,%esi
call   1030 <printf@plt>
addl   $0xffffffff,0x8(%rsp)
je     11bf <main+0x9f>
xor    %ebp,%ebp
mov    %r14,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
mov    %r13,%rcx
xor    %eax,%eax
call   1040 <scanf@plt>
mov    0x10(%rsp),%edx
add    0x14(%rsp),%edx
mov    0xc(%rsp),%ecx
cmp    %ecx,%edx
jl     1170 <main+0x50>
xor    %ebp,%ebp
nopl   0x0(%rax,%rax,1)
mov    %edx,%eax
cltd
idiv   %ecx
add    %eax,%ebp
add    %eax,%edx
cmp    %ecx,%edx
jge    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
store i32 0, i32* %1
%9 = call i32 (i8*, i32*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %2)
br label %10
10: ; preds = %35, %0
%11 = load i32, i32* %2
%12 = icmp ne i32 %11, 0
br i1 %12, label %13, label %40
13: ; preds = %10
%14 = 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)
store i32 0, i32* %6
%15 = load i32, i32* %3
%16 = load i32, i32* %4
%17 = add nsw i32 %15, %16
store i32 %17, i32* %7
br label %18
18: ; preds = %22, %13
%19 = load i32, i32* %7
%20 = load i32, i32* %5
%21 = icmp sge i32 %19, %20
br i1 %21, label %22, label %35
22: ; preds = %18
%23 = load i32, i32* %7
%24 = load i32, i32* %5
%25 = sdiv i32 %23, %24
store i32 %25, i32* %8
%26 = load i32, i32* %7
%27 = load i32, i32* %5
%28 = srem i32 %26, %27
store i32 %28, i32* %7
%29 = load i32, i32* %8
%30 = load i32, i32* %6
%31 = add nsw i32 %30, %29
store i32 %31, i32* %6
%32 = load i32, i32* %8
%33 = load i32, i32* %7
%34 = add nsw i32 %33, %32
store i32 %34, i32* %7
br label %18
35: ; preds = %18
%36 = load i32, i32* %6
%37 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %36)
%38 = load i32, i32* %2
%39 = add nsw i32 %38, -1
store i32 %39, i32* %2
br label %10
40: ; preds = %10
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 eq i32 %10, 0
br i1 %11, label %33, label %12
12: ; preds = %0, %27
%13 = 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)
%14 = load i32, i32* %2
%15 = load i32, i32* %3
%16 = add nsw i32 %15, %14
%17 = load i32, i32* %4
%18 = icmp slt i32 %16, %17
br i1 %18, label %27, label %19
19: ; preds = %12, %19
%20 = phi i32 [ %24, %19 ], [ 0, %12 ]
%21 = phi i32 [ %25, %19 ], [ %16, %12 ]
%22 = sdiv i32 %21, %17
%23 = srem i32 %21, %17
%24 = add nsw i32 %22, %20
%25 = add nsw i32 %23, %22
%26 = icmp slt i32 %25, %17
br i1 %26, label %27, label %19
27: ; preds = %19, %12
%28 = phi i32 [ 0, %12 ], [ %24, %19 ]
%29 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %28)
%30 = load i32, i32* %1
%31 = add nsw i32 %30, -1
store i32 %31, i32* %1
%32 = icmp eq i32 %31, 0
br i1 %32, label %33, label %12
33: ; preds = %27, %0
ret i32 0
}