#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 *, unsigned long);
int /*<<< orphan*/ scanf(char *, ...);
int main() {
  int i, test, farm;
  unsigned long int bigBudge, size, budge, animals;
  scanf("%d", &test);
  for (i = 0; i < test; i++) {
    scanf("%d", &farm);
    bigBudge = 0;
    for (; farm > 0; farm--) {
      scanf("%lu %lu %lu", &size, &animals, &budge);
      if (animals)
        bigBudge += size * budge;
    }
    printf("%lu\n", bigBudge);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xeca(%rip),%rdi
lea    -0xc(%rbp),%rsi
mov    $0x0,%al
call   1040 <scanf@plt>
movl   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11df <main+0xbf>
lea    0xea5(%rip),%rdi
lea    -0x10(%rbp),%rsi
mov    $0x0,%al
call   1040 <scanf@plt>
movq   $0x0,-0x18(%rbp)
cmpl   $0x0,-0x10(%rbp)
jle    11c1 <main+0xa1>
lea    0xe84(%rip),%rdi
lea    -0x20(%rbp),%rsi
lea    -0x30(%rbp),%rdx
lea    -0x28(%rbp),%rcx
mov    $0x0,%al
call   1040 <scanf@plt>
cmpq   $0x0,-0x30(%rbp)
je     11ae <main+0x8e>
mov    -0x20(%rbp),%rax
imul   -0x28(%rbp),%rax
add    -0x18(%rbp),%rax
mov    %rax,-0x18(%rbp)
jmp    11b3 <main+0x93>
mov    -0x10(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x10(%rbp)
jmp    116e <main+0x4e>
mov    -0x18(%rbp),%rsi
lea    0xe43(%rip),%rdi
call   1030 <printf@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1148 <main+0x28>
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    $0x28,%rsp
lea    0xecb(%rip),%rdi
lea    0xc(%rsp),%rsi
xor    %eax,%eax
call   1040 <scanf@plt>
cmpl   $0x0,0xc(%rsp)
jle    11ef <main+0xcf>
lea    0xeb0(%rip),%r12
lea    0x20(%rsp),%r13
lea    0x10(%rsp),%rbx
lea    0x18(%rsp),%rbp
xor    %r14d,%r14d
jmp    118d <main+0x6d>
nopw   0x0(%rax,%rax,1)
xor    %r15d,%r15d
lea    0xe95(%rip),%rdi
mov    %r15,%rsi
call   1030 <printf@plt>
add    $0x1,%r14d
cmp    0xc(%rsp),%r14d
jge    11ef <main+0xcf>
lea    0xe6c(%rip),%rdi
lea    0x8(%rsp),%rsi
xor    %eax,%eax
call   1040 <scanf@plt>
cmpl   $0x0,0x8(%rsp)
jle    1170 <main+0x50>
xor    %r15d,%r15d
nopw   0x0(%rax,%rax,1)
mov    %r12,%rdi
mov    %r13,%rsi
mov    %rbx,%rdx
mov    %rbp,%rcx
xor    %eax,%eax
call   1040 <scanf@plt>
mov    0x10(%rsp),%rax
mov    0x18(%rsp),%rcx
imul   0x20(%rsp),%rcx
test   %rax,%rax
cmove  %rax,%rcx
add    %rcx,%r15
mov    0x8(%rsp),%eax
lea    -0x1(%rax),%ecx
mov    %ecx,0x8(%rsp)
cmp    $0x1,%eax
jg     11b0 <main+0x90>
jmp    1173 <main+0x53>
xor    %eax,%eax
add    $0x28,%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 = [12 x i8] c"%lu %lu %lu\00"
@.str.2 = [5 x i8] c"%lu\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i64
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64
store i32 0, i32* %1
%9 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %3)
store i32 0, i32* %2
br label %10
10: ; preds = %36, %0
%11 = load i32, i32* %2
%12 = load i32, i32* %3
%13 = icmp slt i32 %11, %12
br i1 %13, label %14, label %39
14: ; preds = %10
%15 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %4)
store i64 0, i64* %5
br label %16
16: ; preds = %30, %14
%17 = load i32, i32* %4
%18 = icmp sgt i32 %17, 0
br i1 %18, label %19, label %33
19: ; preds = %16
%20 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i64* %6, i64* %8, i64* %7)
%21 = load i64, i64* %8
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %29
23: ; preds = %19
%24 = load i64, i64* %6
%25 = load i64, i64* %7
%26 = mul i64 %24, %25
%27 = load i64, i64* %5
%28 = add i64 %27, %26
store i64 %28, i64* %5
br label %29
29: ; preds = %23, %19
br label %30
30: ; preds = %29
%31 = load i32, i32* %4
%32 = add nsw i32 %31, -1
store i32 %32, i32* %4
br label %16
33: ; preds = %16
%34 = load i64, i64* %5
%35 = call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i64 %34)
br label %36
36: ; preds = %33
%37 = load i32, i32* %2
%38 = add nsw i32 %37, 1
store i32 %38, i32* %2
br label %10
39: ; preds = %10
ret i32 0
}

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