#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*/ memset(int *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, int, ...);
int /*<<< orphan*/ putchar(char);
int /*<<< orphan*/ scanf(char *, int *);
int term(int *, int *, int);
int main() {
  int n;
  scanf("%d", &n);
  while (n--) {
    int pol1[600], pol2[600];
    memset(pol1, 0, sizeof(int) * 600);
    memset(pol2, 0, sizeof(int) * 600);
    int deg1, deg2, deg_sum;
    scanf("%d", &deg1);
    for (int i = 0; i <= deg1; i++)
      scanf("%d", pol1 + i);
    scanf("%d", &deg2);
    for (int i = 0; i <= deg2; i++)
      scanf("%d", pol2 + i);
    deg_sum = deg1 + deg2;
    printf("%d\n%d", deg_sum, term(pol1, pol2, 0));
    for (int i = 1; i <= deg_sum; i++)
      printf(" %d", term(pol1, pol2, i));
    putchar('\n');
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x12f0,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe9a(%rip),%rdi
lea    -0x8(%rbp),%rsi
call   1070 <scanf@plt>
mov    -0x8(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x8(%rbp)
cmp    $0x0,%eax
je     1325 <main+0x1d5>
lea    -0x970(%rbp),%rdi
xor    %esi,%esi
mov    $0x960,%edx
call   1050 <memset@plt>
lea    -0x12d0(%rbp),%rdi
xor    %esi,%esi
mov    $0x960,%edx
call   1050 <memset@plt>
lea    0xe50(%rip),%rdi
lea    -0x12d4(%rbp),%rsi
call   1070 <scanf@plt>
movl   $0x0,-0x12e0(%rbp)
mov    -0x12e0(%rbp),%eax
cmp    -0x12d4(%rbp),%eax
jg     1210 <main+0xc0>
lea    -0x970(%rbp),%rsi
movslq -0x12e0(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
lea    0xe0c(%rip),%rdi
call   1070 <scanf@plt>
mov    -0x12e0(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x12e0(%rbp)
jmp    11c9 <main+0x79>
lea    0xdec(%rip),%rdi
lea    -0x12d8(%rbp),%rsi
call   1070 <scanf@plt>
movl   $0x0,-0x12e4(%rbp)
mov    -0x12e4(%rbp),%eax
cmp    -0x12d8(%rbp),%eax
jg     1274 <main+0x124>
lea    -0x12d0(%rbp),%rsi
movslq -0x12e4(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
lea    0xda8(%rip),%rdi
call   1070 <scanf@plt>
mov    -0x12e4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x12e4(%rbp)
jmp    122d <main+0xdd>
mov    -0x12d4(%rbp),%eax
add    -0x12d8(%rbp),%eax
mov    %eax,-0x12dc(%rbp)
mov    -0x12dc(%rbp),%eax
mov    %eax,-0x12ec(%rbp)
lea    -0x970(%rbp),%rdi
lea    -0x12d0(%rbp),%rsi
xor    %edx,%edx
call   1060 <term@plt>
mov    -0x12ec(%rbp),%esi
mov    %eax,%edx
lea    0xd4a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x12e8(%rbp)
mov    -0x12e8(%rbp),%eax
cmp    -0x12dc(%rbp),%eax
jg     1316 <main+0x1c6>
lea    -0x970(%rbp),%rdi
lea    -0x12d0(%rbp),%rsi
mov    -0x12e8(%rbp),%edx
call   1060 <term@plt>
mov    %eax,%esi
lea    0xd0b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x12e8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x12e8(%rbp)
jmp    12c7 <main+0x177>
mov    $0xa,%edi
call   1030 <putchar@plt>
jmp    1172 <main+0x22>
xor    %eax,%eax
add    $0x12f0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x12d8,%rsp
lea    0xe9b(%rip),%rdi
lea    0xc(%rsp),%rsi
call   1070 <scanf@plt>
subl   $0x1,0xc(%rsp)
jae    118d <main+0x3d>
xor    %eax,%eax
add    $0x12d8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0x970(%rsp),%r14
lea    0x10(%rsp),%r15
lea    0xe62(%rip),%r12
jmp    11c1 <main+0x71>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    $0xa,%edi
call   1030 <putchar@plt>
subl   $0x1,0xc(%rsp)
jb     1179 <main+0x29>
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x960,%edx
call   1050 <memset@plt>
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x960,%edx
call   1050 <memset@plt>
mov    %r12,%rdi
lea    0x4(%rsp),%rsi
call   1070 <scanf@plt>
cmpl   $0x0,0x4(%rsp)
js     121d <main+0xcd>
mov    $0xffffffffffffffff,%rbx
mov    %r14,%rbp
nopl   (%rax)
mov    %r12,%rdi
mov    %rbp,%rsi
call   1070 <scanf@plt>
movslq 0x4(%rsp),%rax
add    $0x1,%rbx
add    $0x4,%rbp
cmp    %rax,%rbx
jl     1200 <main+0xb0>
mov    %r12,%rdi
lea    0x8(%rsp),%rsi
call   1070 <scanf@plt>
mov    0x8(%rsp),%r13d
test   %r13d,%r13d
js     125d <main+0x10d>
mov    $0xffffffffffffffff,%rbx
mov    %r15,%rbp
xchg   %ax,%ax
mov    %r12,%rdi
mov    %rbp,%rsi
call   1070 <scanf@plt>
movslq 0x8(%rsp),%r13
add    $0x1,%rbx
add    $0x4,%rbp
cmp    %r13,%rbx
jl     1240 <main+0xf0>
add    0x4(%rsp),%r13d
mov    %r14,%rdi
mov    %r15,%rsi
xor    %edx,%edx
call   1060 <term@plt>
lea    0xd8a(%rip),%rdi
mov    %r13d,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <printf@plt>
test   %r13d,%r13d
lea    0xd7a(%rip),%rbx
jle    11b0 <main+0x60>
neg    %r13d
mov    $0x1,%ebp
nopw   0x0(%rax,%rax,1)
mov    %r14,%rdi
mov    %r15,%rsi
mov    %ebp,%edx
call   1060 <term@plt>
mov    %rbx,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0x1(%r13,%rbp,1),%eax
mov    %ebp,%ecx
add    $0x1,%ecx
mov    %ecx,%ebp
cmp    $0x1,%eax
jne    12a0 <main+0x150>
jmp    11b0 <main+0x60>

LLVM IR (-O0):
@.str = [3 x i8] c"%d\00"
@.str.1 = [6 x i8] c"%d\0A%d\00"
@.str.2 = [4 x i8] c" %d\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [600 x i32]
%4 = alloca [600 x i32]
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 0, i32* %1
%11 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %2)
br label %12
12: ; preds = %72, %0
%13 = load i32, i32* %2
%14 = add nsw i32 %13, -1
store i32 %14, i32* %2
%15 = icmp ne i32 %13, 0
br i1 %15, label %16, label %74
16: ; preds = %12
%17 = getelementptr inbounds [600 x i32], [600 x i32]* %3, i64 0, i64 0
%18 = call i32 @memset(i32* %17, i32 0, i32 2400)
%19 = getelementptr inbounds [600 x i32], [600 x i32]* %4, i64 0, i64 0
%20 = call i32 @memset(i32* %19, i32 0, i32 2400)
%21 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %5)
store i32 0, i32* %8
br label %22
22: ; preds = %32, %16
%23 = load i32, i32* %8
%24 = load i32, i32* %5
%25 = icmp sle i32 %23, %24
br i1 %25, label %26, label %35
26: ; preds = %22
%27 = getelementptr inbounds [600 x i32], [600 x i32]* %3, i64 0, i64 0
%28 = load i32, i32* %8
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i32, i32* %27, i64 %29
%31 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %30)
br label %32
32: ; preds = %26
%33 = load i32, i32* %8
%34 = add nsw i32 %33, 1
store i32 %34, i32* %8
br label %22
35: ; preds = %22
%36 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %6)
store i32 0, i32* %9
br label %37
37: ; preds = %47, %35
%38 = load i32, i32* %9
%39 = load i32, i32* %6
%40 = icmp sle i32 %38, %39
br i1 %40, label %41, label %50
41: ; preds = %37
%42 = getelementptr inbounds [600 x i32], [600 x i32]* %4, i64 0, i64 0
%43 = load i32, i32* %9
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds i32, i32* %42, i64 %44
%46 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %45)
br label %47
47: ; preds = %41
%48 = load i32, i32* %9
%49 = add nsw i32 %48, 1
store i32 %49, i32* %9
br label %37
50: ; preds = %37
%51 = load i32, i32* %5
%52 = load i32, i32* %6
%53 = add nsw i32 %51, %52
store i32 %53, i32* %7
%54 = load i32, i32* %7
%55 = getelementptr inbounds [600 x i32], [600 x i32]* %3, i64 0, i64 0
%56 = getelementptr inbounds [600 x i32], [600 x i32]* %4, i64 0, i64 0
%57 = call i32 @term(i32* %55, i32* %56, i32 0)
%58 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 %54, i32 %57)
store i32 1, i32* %10
br label %59
59: ; preds = %69, %50
%60 = load i32, i32* %10
%61 = load i32, i32* %7
%62 = icmp sle i32 %60, %61
br i1 %62, label %63, label %72
63: ; preds = %59
%64 = getelementptr inbounds [600 x i32], [600 x i32]* %3, i64 0, i64 0
%65 = getelementptr inbounds [600 x i32], [600 x i32]* %4, i64 0, i64 0
%66 = load i32, i32* %10
%67 = call i32 @term(i32* %64, i32* %65, i32 %66)
%68 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %67)
br label %69
69: ; preds = %63
%70 = load i32, i32* %10
%71 = add nsw i32 %70, 1
store i32 %71, i32* %10
br label %59
72: ; preds = %59
%73 = call i32 @putchar(i8 signext 10)
br label %12
74: ; preds = %12
ret i32 0
}

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