#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*/ free(int *);
scalar_t__ malloc(int);
unsigned long merge_sort(int *, int *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, unsigned long);
int /*<<< orphan*/ scanf(char *, int *);
int main() {
  int t, n, i, j, *values, *scratch;
  scanf("%d", &t);
  unsigned long answers[t];
  for (i = 0; i < t; ++i) {
    scanf("%d", &n);
    values = (int *)malloc(n * sizeof(int));
    scratch = (int *)malloc((n / 2 + 1) * sizeof(int));
    for (j = 0; j < n; ++j) {
      scanf("%d", &values[j]);
    }
    answers[i] = merge_sort(values, scratch, 0, n - 1);
    free(scratch);
    free(values);
  }
  for (i = 0; i < t; ++i) {
    printf("%lu\n", answers[i]);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe9a(%rip),%rdi
lea    -0x8(%rbp),%rsi
call   1070 <scanf@plt>
mov    -0x8(%rbp),%eax
mov    %rsp,%rcx
mov    %rcx,-0x30(%rbp)
lea    0xf(,%rax,8),%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x40(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x38(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    126b <main+0x11b>
lea    0xe50(%rip),%rdi
lea    -0xc(%rbp),%rsi
call   1070 <scanf@plt>
movslq -0xc(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0xc(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
add    $0x1,%eax
cltq
shl    $0x2,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    1227 <main+0xd7>
mov    -0x20(%rbp),%rsi
movslq -0x14(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
lea    0xdec(%rip),%rdi
call   1070 <scanf@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11f2 <main+0xa2>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0xc(%rbp),%ecx
sub    $0x1,%ecx
xor    %edx,%edx
call   1060 <merge_sort@plt>
mov    %rax,%rdx
mov    -0x40(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    119d <main+0x4d>
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    12a4 <main+0x154>
mov    -0x40(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
lea    0xd72(%rip),%rdi
call   1040 <printf@plt>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1272 <main+0x122>
movl   $0x0,-0x4(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
lea    0xe98(%rip),%rdi
lea    -0x30(%rbp),%rsi
call   1070 <scanf@plt>
mov    %rsp,%r15
mov    -0x30(%rbp),%eax
mov    %rsp,%rcx
lea    0xf(,%rax,8),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rcx
mov    %rcx,%rsp
cmpl   $0x0,-0x30(%rbp)
jle    1289 <main+0x139>
mov    %rcx,-0x38(%rbp)
mov    %r15,-0x40(%rbp)
xor    %r14d,%r14d
lea    0xe58(%rip),%r13
jmp    11e5 <main+0x95>
nopw   0x0(%rax,%rax,1)
add    $0xffffffff,%ecx
mov    %rbx,%rdi
mov    %r12,%rsi
xor    %edx,%edx
call   1060 <merge_sort@plt>
mov    -0x38(%rbp),%rcx
mov    %rax,(%rcx,%r14,8)
mov    %r12,%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
add    $0x1,%r14
movslq -0x30(%rbp),%rax
cmp    %rax,%r14
jge    1255 <main+0x105>
mov    %r13,%rdi
lea    -0x2c(%rbp),%rsi
call   1070 <scanf@plt>
mov    -0x2c(%rbp),%edi
shl    $0x2,%edi
call   1050 <malloc@plt>
mov    %rax,%rbx
mov    -0x2c(%rbp),%eax
mov    %eax,%ecx
shr    $0x1f,%ecx
add    %eax,%ecx
and    $0xfffffffe,%ecx
lea    (%rcx,%rcx,1),%edi
add    $0x4,%edi
call   1050 <malloc@plt>
mov    %rax,%r12
mov    -0x2c(%rbp),%ecx
test   %ecx,%ecx
jle    11b0 <main+0x60>
mov    %rbx,-0x48(%rbp)
xor    %r15d,%r15d
nopl   0x0(%rax,%rax,1)
mov    %r13,%rdi
mov    %rbx,%rsi
call   1070 <scanf@plt>
add    $0x1,%r15
movslq -0x2c(%rbp),%rcx
add    $0x4,%rbx
cmp    %rcx,%r15
jl     1230 <main+0xe0>
mov    -0x48(%rbp),%rbx
jmp    11b0 <main+0x60>
test   %eax,%eax
mov    -0x40(%rbp),%r15
mov    -0x38(%rbp),%r12
jle    1289 <main+0x139>
xor    %ebx,%ebx
lea    0xd99(%rip),%r14
nopw   0x0(%rax,%rax,1)
mov    (%r12,%rbx,8),%rsi
mov    %r14,%rdi
call   1040 <printf@plt>
add    $0x1,%rbx
movslq -0x30(%rbp),%rax
cmp    %rax,%rbx
jl     1270 <main+0x120>
mov    %r15,%rsp
xor    %eax,%eax
lea    -0x28(%rbp),%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 = [5 x i8] c"%lu\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 i8*
%9 = alloca i64
store i32 0, i32* %1
%10 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %2)
%11 = load i32, i32* %2
%12 = zext i32 %11 to i64
%13 = call i8* @llvm.stacksave()
store i8* %13, i8** %8
%14 = alloca i64, i64 %12
store i64 %12, i64* %9
store i32 0, i32* %4
br label %15
15: ; preds = %61, %0
%16 = load i32, i32* %4
%17 = load i32, i32* %2
%18 = icmp slt i32 %16, %17
br i1 %18, label %19, label %64
19: ; preds = %15
%20 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %3)
%21 = load i32, i32* %3
%22 = sext i32 %21 to i64
%23 = mul i64 %22, 4
%24 = trunc i64 %23 to i32
%25 = call i64 @malloc(i32 %24)
%26 = inttoptr i64 %25 to i32*
store i32* %26, i32** %6
%27 = load i32, i32* %3
%28 = sdiv i32 %27, 2
%29 = add nsw i32 %28, 1
%30 = sext i32 %29 to i64
%31 = mul i64 %30, 4
%32 = trunc i64 %31 to i32
%33 = call i64 @malloc(i32 %32)
%34 = inttoptr i64 %33 to i32*
store i32* %34, i32** %7
store i32 0, i32* %5
br label %35
35: ; preds = %45, %19
%36 = load i32, i32* %5
%37 = load i32, i32* %3
%38 = icmp slt i32 %36, %37
br i1 %38, label %39, label %48
39: ; preds = %35
%40 = load i32*, i32** %6
%41 = load i32, i32* %5
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds i32, i32* %40, i64 %42
%44 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %43)
br label %45
45: ; preds = %39
%46 = load i32, i32* %5
%47 = add nsw i32 %46, 1
store i32 %47, i32* %5
br label %35
48: ; preds = %35
%49 = load i32*, i32** %6
%50 = load i32*, i32** %7
%51 = load i32, i32* %3
%52 = sub nsw i32 %51, 1
%53 = call i64 @merge_sort(i32* %49, i32* %50, i32 0, i32 %52)
%54 = load i32, i32* %4
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds i64, i64* %14, i64 %55
store i64 %53, i64* %56
%57 = load i32*, i32** %7
%58 = call i32 @free(i32* %57)
%59 = load i32*, i32** %6
%60 = call i32 @free(i32* %59)
br label %61
61: ; preds = %48
%62 = load i32, i32* %4
%63 = add nsw i32 %62, 1
store i32 %63, i32* %4
br label %15
64: ; preds = %15
store i32 0, i32* %4
br label %65
65: ; preds = %75, %64
%66 = load i32, i32* %4
%67 = load i32, i32* %2
%68 = icmp slt i32 %66, %67
br i1 %68, label %69, label %78
69: ; preds = %65
%70 = load i32, i32* %4
%71 = sext i32 %70 to i64
%72 = getelementptr inbounds i64, i64* %14, i64 %71
%73 = load i64, i64* %72
%74 = call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i64 %73)
br label %75
75: ; preds = %69
%76 = load i32, i32* %4
%77 = add nsw i32 %76, 1
store i32 %77, i32* %4
br label %65
78: ; preds = %65
store i32 0, i32* %1
%79 = load i8*, i8** %8
%80 = load i32, i32* %1
ret i32 %80
}

LLVM IR (-O2):
@.str = [3 x i8] c"%d\00"
@.str.1 = [5 x i8] c"%lu\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = bitcast i32* %1 to i8*
%4 = bitcast i32* %2 to i8*
%5 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %1)
%6 = load i32, i32* %1
%7 = zext i32 %6 to i64
%8 = call i8* @llvm.stacksave()
%9 = alloca i64, i64 %7
%10 = load i32, i32* %1
%11 = icmp sgt i32 %10, 0
br i1 %11, label %14, label %57
12: ; preds = %37
%13 = icmp sgt i32 %45, 0
br i1 %13, label %48, label %57
14: ; preds = %0, %37
%15 = phi i64 [ %44, %37 ], [ 0, %0 ]
%16 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %2)
%17 = load i32, i32* %2
%18 = shl i32 %17, 2
%19 = call i64 @malloc(i32 %18)
%20 = inttoptr i64 %19 to i32*
%21 = load i32, i32* %2
%22 = sdiv i32 %21, 2
%23 = shl i32 %22, 2
%24 = add i32 %23, 4
%25 = call i64 @malloc(i32 %24)
%26 = inttoptr i64 %25 to i32*
%27 = load i32, i32* %2
%28 = icmp sgt i32 %27, 0
br i1 %28, label %29, label %37
29: ; preds = %14, %29
%30 = phi i64 [ %33, %29 ], [ 0, %14 ]
%31 = getelementptr inbounds i32, i32* %20, i64 %30
%32 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %31)
%33 = add nuw nsw i64 %30, 1
%34 = load i32, i32* %2
%35 = sext i32 %34 to i64
%36 = icmp slt i64 %33, %35
br i1 %36, label %29, label %37
37: ; preds = %29, %14
%38 = phi i32 [ %27, %14 ], [ %34, %29 ]
%39 = add nsw i32 %38, -1
%40 = call i64 @merge_sort(i32* %20, i32* %26, i32 0, i32 %39)
%41 = getelementptr inbounds i64, i64* %9, i64 %15
store i64 %40, i64* %41
%42 = call i32 @free(i32* %26)
%43 = call i32 @free(i32* %20)
%44 = add nuw nsw i64 %15, 1
%45 = load i32, i32* %1
%46 = sext i32 %45 to i64
%47 = icmp slt i64 %44, %46
br i1 %47, label %14, label %12
48: ; preds = %12, %48
%49 = phi i64 [ %53, %48 ], [ 0, %12 ]
%50 = getelementptr inbounds i64, i64* %9, i64 %49
%51 = load i64, i64* %50
%52 = call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i64 %51)
%53 = add nuw nsw i64 %49, 1
%54 = load i32, i32* %1
%55 = sext i32 %54 to i64
%56 = icmp slt i64 %53, %55
br i1 %56, label %48, label %57
57: ; preds = %48, %0, %12
ret i32 0
}