int main(void) {
  int i, b, n, p, q, r;
  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    scanf("%d %d %d", &p, &q, &r);
    for (b = max(max(gb(p), gb(q)), gb(r)); b <= 16; b++) {
      if (rb(p, b) * rb(q, b) == rb(r, b)) {
        printf("%d\n", b);
        break;
      } else if (b == 16)
        printf("0\n");
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xea0(%rip),%rdi
lea    -0x10(%rbp),%rsi
mov    $0x0,%al
call   1060 <scanf@plt>
movl   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%eax
cmp    -0x10(%rbp),%eax
jge    1282 <main+0x132>
lea    0xe75(%rip),%rdi
lea    -0x14(%rbp),%rsi
lea    -0x18(%rbp),%rdx
lea    -0x1c(%rbp),%rcx
mov    $0x0,%al
call   1060 <scanf@plt>
mov    -0x14(%rbp),%edi
mov    $0x0,%al
call   1070 <gb@plt>
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%edi
mov    $0x0,%al
call   1070 <gb@plt>
mov    -0x24(%rbp),%edi
mov    %eax,%esi
mov    $0x0,%al
call   1040 <max@plt>
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
mov    $0x0,%al
call   1070 <gb@plt>
mov    -0x20(%rbp),%edi
mov    %eax,%esi
mov    $0x0,%al
call   1040 <max@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x10,-0xc(%rbp)
jg     126f <main+0x11f>
mov    -0x14(%rbp),%edi
mov    -0xc(%rbp),%esi
mov    $0x0,%al
call   1050 <rb@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x18(%rbp),%edi
mov    -0xc(%rbp),%esi
mov    $0x0,%al
call   1050 <rb@plt>
mov    %eax,%ecx
mov    -0x2c(%rbp),%eax
imul   %ecx,%eax
mov    %eax,-0x28(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0xc(%rbp),%esi
mov    $0x0,%al
call   1050 <rb@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%eax
cmp    %ecx,%eax
jne    123f <main+0xef>
mov    -0xc(%rbp),%esi
lea    0xdd6(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    126f <main+0x11f>
cmpl   $0x10,-0xc(%rbp)
jne    1257 <main+0x107>
lea    0xdbd(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    125c <main+0x10c>
jmp    1261 <main+0x111>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    11dd <main+0x8d>
jmp    1274 <main+0x124>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1178 <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    $0x18,%rsp
lea    0xe91(%rip),%rdi
lea    0x14(%rsp),%rsi
xor    %eax,%eax
call   1070 <scanf@plt>
cmpl   $0x0,0x14(%rsp)
jle    12c2 <main+0x162>
lea    0xe6d(%rip),%r15
lea    0x10(%rsp),%r14
lea    0xc(%rsp),%r13
lea    0x8(%rsp),%rbx
xor    %r12d,%r12d
jmp    11de <main+0x7e>
nopw   0x0(%rax,%rax,1)
lea    0xe52(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0x10(%rsp),%r14
lea    0xc(%rsp),%r13
lea    0x8(%rsp),%rbx
add    $0x1,%r12d
cmp    0x14(%rsp),%r12d
jge    12c2 <main+0x162>
mov    %r15,%rdi
mov    %r14,%rsi
mov    %r13,%rdx
mov    %rbx,%rcx
xor    %eax,%eax
call   1070 <scanf@plt>
mov    0x10(%rsp),%edi
xor    %eax,%eax
call   1080 <gb@plt>
mov    %eax,%ebp
mov    0xc(%rsp),%edi
xor    %eax,%eax
call   1080 <gb@plt>
mov    %ebp,%edi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <max@plt>
mov    %eax,%ebp
mov    0x8(%rsp),%edi
xor    %eax,%eax
call   1080 <gb@plt>
mov    %ebp,%edi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <max@plt>
cmp    $0x10,%eax
jg     11cf <main+0x6f>
mov    %eax,%ebp
mov    0x10(%rsp),%edi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <rb@plt>
mov    %eax,%r13d
mov    0xc(%rsp),%edi
mov    %ebp,%esi
xor    %eax,%eax
call   1060 <rb@plt>
mov    %eax,%r14d
imul   %r13d,%r14d
mov    0x8(%rsp),%edi
mov    %ebp,%esi
xor    %eax,%eax
call   1060 <rb@plt>
cmp    %eax,%r14d
je     11b0 <main+0x50>
nopl   (%rax)
cmp    $0x10,%ebp
je     12b1 <main+0x151>
add    $0x1,%ebp
mov    0x10(%rsp),%edi
mov    %ebp,%esi
xor    %eax,%eax
call   1060 <rb@plt>
mov    %eax,%r14d
mov    0xc(%rsp),%edi
mov    %ebp,%esi
xor    %eax,%eax
call   1060 <rb@plt>
mov    %eax,%ebx
imul   %r14d,%ebx
mov    0x8(%rsp),%edi
mov    %ebp,%esi
xor    %eax,%eax
call   1060 <rb@plt>
cmp    %eax,%ebx
jne    1270 <main+0x110>
jmp    11b0 <main+0x50>
lea    0xd55(%rip),%rdi
call   1030 <puts@plt>
jmp    11c0 <main+0x60>
xor    %eax,%eax
add    $0x18,%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 = [9 x i8] c"%d %d %d\00"
@.str.2 = [4 x i8] c"%d\0A\00"
@.str.3 = [3 x i8] c"0\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
store i32 0, i32* %1
%8 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %4)
store i32 0, i32* %2
br label %9
9: ; preds = %52, %0
%10 = load i32, i32* %2
%11 = load i32, i32* %4
%12 = icmp slt i32 %10, %11
br i1 %12, label %13, label %55
13: ; preds = %9
%14 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32* %5, i32* %6, i32* %7)
%15 = load i32, i32* %5
%16 = call i32 (i32, ...) bitcast (i32 (...)* @gb to i32 (i32, ...)*)(i32 %15)
%17 = load i32, i32* %6
%18 = call i32 (i32, ...) bitcast (i32 (...)* @gb to i32 (i32, ...)*)(i32 %17)
%19 = call i32 (i32, i32, ...) bitcast (i32 (...)* @max to i32 (i32, i32, ...)*)(i32 %16, i32 %18)
%20 = load i32, i32* %7
%21 = call i32 (i32, ...) bitcast (i32 (...)* @gb to i32 (i32, ...)*)(i32 %20)
%22 = call i32 (i32, i32, ...) bitcast (i32 (...)* @max to i32 (i32, i32, ...)*)(i32 %19, i32 %21)
store i32 %22, i32* %3
br label %23
23: ; preds = %48, %13
%24 = load i32, i32* %3
%25 = icmp sle i32 %24, 16
br i1 %25, label %26, label %51
26: ; preds = %23
%27 = load i32, i32* %5
%28 = load i32, i32* %3
%29 = call i32 (i32, i32, ...) bitcast (i32 (...)* @rb to i32 (i32, i32, ...)*)(i32 %27, i32 %28)
%30 = load i32, i32* %6
%31 = load i32, i32* %3
%32 = call i32 (i32, i32, ...) bitcast (i32 (...)* @rb to i32 (i32, i32, ...)*)(i32 %30, i32 %31)
%33 = mul nsw i32 %29, %32
%34 = load i32, i32* %7
%35 = load i32, i32* %3
%36 = call i32 (i32, i32, ...) bitcast (i32 (...)* @rb to i32 (i32, i32, ...)*)(i32 %34, i32 %35)
%37 = icmp eq i32 %33, %36
br i1 %37, label %38, label %41
38: ; preds = %26
%39 = load i32, i32* %3
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %39)
br label %51
41: ; preds = %26
%42 = load i32, i32* %3
%43 = icmp eq i32 %42, 16
br i1 %43, label %44, label %46
44: ; preds = %41
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
br label %46
46: ; preds = %44, %41
br label %47
47: ; preds = %46
br label %48
48: ; preds = %47
%49 = load i32, i32* %3
%50 = add nsw i32 %49, 1
store i32 %50, i32* %3
br label %23
51: ; preds = %38, %23
br label %52
52: ; preds = %51
%53 = load i32, i32* %2
%54 = add nsw i32 %53, 1
store i32 %54, i32* %2
br label %9
55: ; preds = %9
ret i32 0
}

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