#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*/ scanf(char *, int *);
int /*<<< orphan*/ stableMarriage(int **, int);
int main() {
  int testcases;
  scanf("%d", &testcases);
  while (testcases-- > 0) {
    int a;
    scanf("%d", &a);
    int prefer[2 * 1000][1000];
    for (int i = 0; i < 2 * a; i++) {
      for (int j = 0; j < a; j++) {
        int jj;
        scanf("%d", &jj);
        prefer[i][j] = jj;
      }
    }
    stableMarriage(prefer, a);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x7a1220,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xec7(%rip),%rdi
lea    -0x8(%rbp),%rsi
call   1040 <scanf@plt>
mov    -0x8(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x8(%rbp)
cmp    $0x0,%eax
jle    1219 <main+0xf9>
lea    0xea3(%rip),%rdi
lea    -0xc(%rbp),%rsi
call   1040 <scanf@plt>
movl   $0x0,-0x7a1214(%rbp)
mov    -0x7a1214(%rbp),%eax
mov    -0xc(%rbp),%ecx
shl    $0x1,%ecx
cmp    %ecx,%eax
jge    1205 <main+0xe5>
movl   $0x0,-0x7a1218(%rbp)
mov    -0x7a1218(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11ec <main+0xcc>
lea    0xe5c(%rip),%rdi
lea    -0x7a121c(%rbp),%rsi
call   1040 <scanf@plt>
mov    -0x7a121c(%rbp),%edx
movslq -0x7a1214(%rbp),%rcx
lea    -0x7a1210(%rbp),%rax
imul   $0xfa0,%rcx,%rcx
add    %rcx,%rax
movslq -0x7a1218(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x7a1218(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x7a1218(%rbp)
jmp    118e <main+0x6e>
jmp    11f1 <main+0xd1>
mov    -0x7a1214(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x7a1214(%rbp)
jmp    1170 <main+0x50>
lea    -0x7a1210(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1030 <stableMarriage@plt>
jmp    1142 <main+0x22>
xor    %eax,%eax
add    $0x7a1220,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x7a1218,%rsp
lea    0xec8(%rip),%rdi
lea    0x4(%rsp),%rsi
call   1040 <scanf@plt>
mov    0x4(%rsp),%eax
lea    -0x1(%rax),%ecx
mov    %ecx,0x4(%rsp)
test   %eax,%eax
jle    11f3 <main+0xd3>
lea    0xea4(%rip),%r12
lea    0x10(%rsp),%r15
lea    0xc(%rsp),%r13
jmp    1187 <main+0x67>
nopl   0x0(%rax,%rax,1)
mov    %r15,%rdi
call   1030 <stableMarriage@plt>
mov    0x4(%rsp),%eax
lea    -0x1(%rax),%ecx
mov    %ecx,0x4(%rsp)
test   %eax,%eax
jle    11f3 <main+0xd3>
mov    %r12,%rdi
lea    0x8(%rsp),%rsi
call   1040 <scanf@plt>
mov    0x8(%rsp),%esi
test   %esi,%esi
jle    1170 <main+0x50>
mov    %r15,%rbx
xor    %ebp,%ebp
jmp    11c5 <main+0xa5>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
add    $0x1,%rbp
lea    (%rsi,%rsi,1),%eax
cltq
add    $0xfa0,%rbx
cmp    %rax,%rbp
jge    1170 <main+0x50>
test   %esi,%esi
jle    11b0 <main+0x90>
xor    %r14d,%r14d
nopl   0x0(%rax)
mov    %r12,%rdi
mov    %r13,%rsi
call   1040 <scanf@plt>
mov    0xc(%rsp),%eax
mov    %eax,(%rbx,%r14,4)
add    $0x1,%r14
movslq 0x8(%rsp),%rsi
cmp    %rsi,%r14
jl     11d0 <main+0xb0>
jmp    11b0 <main+0x90>
xor    %eax,%eax
add    $0x7a1218,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [3 x i8] c"%d\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [2000 x [1000 x i32]]
%4 = alloca i32
%5 = bitcast i32* %1 to i8*
%6 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %1)
%7 = load i32, i32* %1
%8 = add nsw i32 %7, -1
store i32 %8, i32* %1
%9 = icmp sgt i32 %7, 0
br i1 %9, label %10, label %44
10: ; preds = %0
%11 = bitcast i32* %2 to i8*
%12 = bitcast [2000 x [1000 x i32]]* %3 to i8*
%13 = bitcast i32* %4 to i8*
%14 = bitcast [2000 x [1000 x i32]]* %3 to i32**
br label %15
15: ; preds = %10, %23
%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 = icmp sgt i32 %17, 0
br i1 %18, label %19, label %23
19: ; preds = %15, %29
%20 = phi i32 [ %30, %29 ], [ %17, %15 ]
%21 = phi i64 [ %31, %29 ], [ 0, %15 ]
%22 = icmp sgt i32 %20, 0
br i1 %22, label %35, label %29
23: ; preds = %29, %15
%24 = phi i32 [ %17, %15 ], [ %30, %29 ]
%25 = call i32 @stableMarriage(i32** %14, i32 %24)
%26 = load i32, i32* %1
%27 = add nsw i32 %26, -1
store i32 %27, i32* %1
%28 = icmp sgt i32 %26, 0
br i1 %28, label %15, label %44
29: ; preds = %35, %19
%30 = phi i32 [ %20, %19 ], [ %41, %35 ]
%31 = add nuw nsw i64 %21, 1
%32 = shl nsw i32 %30, 1
%33 = sext i32 %32 to i64
%34 = icmp slt i64 %31, %33
br i1 %34, label %19, label %23
35: ; preds = %19, %35
%36 = phi i64 [ %40, %35 ], [ 0, %19 ]
%37 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %4)
%38 = load i32, i32* %4
%39 = getelementptr inbounds [2000 x [1000 x i32]], [2000 x [1000 x i32]]* %3, i64 0, i64 %21, i64 %36
store i32 %38, i32* %39
%40 = add nuw nsw i64 %36, 1
%41 = load i32, i32* %2
%42 = sext i32 %41 to i64
%43 = icmp slt i64 %40, %42
br i1 %43, label %35, label %29
44: ; preds = %23, %0
ret i32 0
}