#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*/ getMinTransaction(unsigned long long *, unsigned long long);
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *, int /*<<< orphan*/);
int /*<<< orphan*/ scanf(char *, unsigned long long *);
int main() {
  unsigned long long n, i, t;
  scanf("%lu", &t);
  while (t--) {
    unsigned long long *arr, tmin = 0, acSum = 0, surplus = 0, outOfOrder = 0;
    scanf("%lu", &n);
    arr = (unsigned long long *)malloc(sizeof(unsigned long long) * n);
    for (i = 0; i < n; i++) {
      scanf("%lu", &arr[i]);
    }
    printf("%lu\n", getMinTransaction(arr, n));
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xeaa(%rip),%rdi
lea    -0x20(%rbp),%rsi
call   1060 <scanf@plt>
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,-0x20(%rbp)
cmp    $0x0,%rax
je     121d <main+0xdd>
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
movq   $0x0,-0x40(%rbp)
movq   $0x0,-0x48(%rbp)
lea    0xe61(%rip),%rdi
lea    -0x10(%rbp),%rsi
call   1060 <scanf@plt>
mov    -0x10(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%edi
call   1040 <malloc@plt>
mov    %rax,-0x28(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%rax
cmp    -0x10(%rbp),%rax
jae    11fd <main+0xbd>
mov    -0x28(%rbp),%rsi
mov    -0x18(%rbp),%rax
shl    $0x3,%rax
add    %rax,%rsi
lea    0xe19(%rip),%rdi
call   1060 <scanf@plt>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    11c3 <main+0x83>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1050 <getMinTransaction@plt>
mov    %eax,%esi
lea    0xdf1(%rip),%rdi
call   1030 <printf@plt>
jmp    115f <main+0x1f>
xor    %eax,%eax
add    $0x50,%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    0xeab(%rip),%rdi
lea    0x10(%rsp),%rsi
call   1060 <scanf@plt>
subq   $0x1,0x10(%rsp)
jae    1178 <main+0x38>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe81(%rip),%r13
lea    0x8(%rsp),%r14
lea    0xe79(%rip),%r15
jmp    11ac <main+0x6c>
nopl   (%rax)
xor    %esi,%esi
mov    %r12,%rdi
call   1050 <getMinTransaction@plt>
mov    %r15,%rdi
mov    %eax,%esi
call   1030 <printf@plt>
subq   $0x1,0x10(%rsp)
jb     1167 <main+0x27>
mov    %r13,%rdi
mov    %r14,%rsi
call   1060 <scanf@plt>
mov    0x8(%rsp),%edi
shl    $0x3,%edi
call   1040 <malloc@plt>
mov    %rax,%r12
cmpq   $0x0,0x8(%rsp)
je     1190 <main+0x50>
mov    %r12,%rbx
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r13,%rdi
mov    %rbx,%rsi
call   1060 <scanf@plt>
add    $0x1,%rbp
mov    0x8(%rsp),%rsi
add    $0x8,%rbx
cmp    %rsi,%rbp
jb     11e0 <main+0xa0>
jmp    1192 <main+0x52>

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

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