#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*/ find_prime_factors(int, int *, int, int *, int *);
int /*<<< orphan*/ find_primes(int /*<<< orphan*/, int *, int *);
int /*<<< orphan*/ pow(int, int);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ scanf(char *, int *);
int /*<<< orphan*/ sqrt(int /*<<< orphan*/);
int sum_digits(int);
int main() {
  int primes[5 * 1 << 10], num_primes;
  int factors[5 * 1 << 10], num_factors;
  int num_cases, num;
  int sum_1, sum_2;
  int i;
  find_primes(sqrt(pow(10, 9)), primes, &num_primes);
  scanf("%d\n", &num_cases);
  while (num_cases-- > 0) {
    scanf("%d\n", &num);
    while (num++) {
      find_prime_factors(num, primes, num_primes, factors, &num_factors);
      sum_1 = sum_digits(num);
      for (sum_2 = 0, i = 0; i < num_factors; i++) {
        sum_2 += sum_digits(factors[i]);
      }
      if (num_factors != 1 && sum_1 == sum_2) {
        printf("%d\n", num);
        break;
      }
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa040,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0xa,%edi
mov    $0x9,%esi
call   1080 <pow@plt>
mov    %eax,%edi
call   1090 <sqrt@plt>
mov    %eax,%edi
lea    -0x5010(%rbp),%rsi
lea    -0x5014(%rbp),%rdx
call   1030 <find_primes@plt>
lea    0xe4c(%rip),%rdi
lea    -0xa028(%rbp),%rsi
call   1070 <scanf@plt>
mov    -0xa028(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0xa028(%rbp)
cmp    $0x0,%eax
jle    12d7 <main+0x167>
lea    0xe1f(%rip),%rdi
lea    -0xa02c(%rbp),%rsi
call   1070 <scanf@plt>
mov    -0xa02c(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0xa02c(%rbp)
cmp    $0x0,%eax
je     12d2 <main+0x162>
mov    -0xa02c(%rbp),%edi
lea    -0x5010(%rbp),%rsi
mov    -0x5014(%rbp),%edx
lea    -0xa020(%rbp),%rcx
lea    -0xa024(%rbp),%r8
call   1050 <find_prime_factors@plt>
mov    -0xa02c(%rbp),%edi
call   1060 <sum_digits@plt>
mov    %eax,-0xa030(%rbp)
movl   $0x0,-0xa034(%rbp)
movl   $0x0,-0xa038(%rbp)
mov    -0xa038(%rbp),%eax
cmp    -0xa024(%rbp),%eax
jge    1297 <main+0x127>
movslq -0xa038(%rbp),%rax
mov    -0xa020(%rbp,%rax,4),%edi
call   1060 <sum_digits@plt>
add    -0xa034(%rbp),%eax
mov    %eax,-0xa034(%rbp)
mov    -0xa038(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xa038(%rbp)
jmp    1252 <main+0xe2>
cmpl   $0x1,-0xa024(%rbp)
je     12cd <main+0x15d>
mov    -0xa030(%rbp),%eax
cmp    -0xa034(%rbp),%eax
jne    12cd <main+0x15d>
mov    -0xa02c(%rbp),%esi
lea    0xd3d(%rip),%rdi
call   1040 <printf@plt>
jmp    12d2 <main+0x162>
jmp    11ed <main+0x7d>
jmp    11c0 <main+0x50>
xor    %eax,%eax
add    $0xa040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0xa018,%rsp
mov    $0xa,%edi
mov    $0x9,%esi
call   1080 <pow@plt>
mov    %eax,%edi
call   1090 <sqrt@plt>
lea    0x5010(%rsp),%rsi
lea    0xc(%rsp),%rdx
mov    %eax,%edi
call   1030 <find_primes@plt>
lea    0xe4e(%rip),%rdi
lea    0x4(%rsp),%rsi
call   1070 <scanf@plt>
mov    0x4(%rsp),%eax
lea    -0x1(%rax),%ecx
mov    %ecx,0x4(%rsp)
test   %eax,%eax
jle    12b1 <main+0x141>
lea    0x5010(%rsp),%r12
lea    0x10(%rsp),%r13
lea    0x8(%rsp),%rbx
jmp    1203 <main+0x93>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    0x4(%rsp),%eax
lea    -0x1(%rax),%ecx
mov    %ecx,0x4(%rsp)
test   %eax,%eax
jle    12b1 <main+0x141>
lea    0xdf6(%rip),%rdi
mov    %rsp,%rsi
call   1070 <scanf@plt>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    (%rsp),%eax
lea    0x1(%rax),%edi
mov    %edi,(%rsp)
test   %eax,%eax
je     11f0 <main+0x80>
mov    0xc(%rsp),%edx
mov    %r12,%rsi
mov    %r13,%rcx
mov    %rbx,%r8
call   1050 <find_prime_factors@plt>
mov    (%rsp),%edi
call   1060 <sum_digits@plt>
mov    %eax,%ebp
mov    0x8(%rsp),%eax
test   %eax,%eax
jle    1290 <main+0x120>
xor    %r15d,%r15d
xor    %r14d,%r14d
nopw   0x0(%rax,%rax,1)
mov    0x10(%rsp,%r15,4),%edi
call   1060 <sum_digits@plt>
add    %eax,%r14d
add    $0x1,%r15
movslq 0x8(%rsp),%rax
cmp    %rax,%r15
jl     1260 <main+0xf0>
cmp    $0x1,%eax
jne    1298 <main+0x128>
jmp    1220 <main+0xb0>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
xor    %r14d,%r14d
cmp    $0x1,%eax
je     1220 <main+0xb0>
cmp    %r14d,%ebp
jne    1220 <main+0xb0>
mov    (%rsp),%esi
lea    0xd59(%rip),%rdi
call   1040 <printf@plt>
jmp    11f0 <main+0x80>
xor    %eax,%eax
add    $0xa018,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [4 x i8] c"%d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [5120 x i32]
%3 = alloca i32
%4 = alloca [5120 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 @pow(i32 10, i32 9)
%12 = call i32 @sqrt(i32 %11)
%13 = getelementptr inbounds [5120 x i32], [5120 x i32]* %2, i64 0, i64 0
%14 = call i32 @find_primes(i32 %12, i32* %13, i32* %3)
%15 = call i32 @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32* %6)
br label %16
16: ; preds = %60, %0
%17 = load i32, i32* %6
%18 = add nsw i32 %17, -1
store i32 %18, i32* %6
%19 = icmp sgt i32 %17, 0
br i1 %19, label %20, label %61
20: ; preds = %16
%21 = call i32 @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32* %7)
br label %22
22: ; preds = %59, %20
%23 = load i32, i32* %7
%24 = add nsw i32 %23, 1
store i32 %24, i32* %7
%25 = icmp ne i32 %23, 0
br i1 %25, label %26, label %60
26: ; preds = %22
%27 = load i32, i32* %7
%28 = getelementptr inbounds [5120 x i32], [5120 x i32]* %2, i64 0, i64 0
%29 = load i32, i32* %3
%30 = getelementptr inbounds [5120 x i32], [5120 x i32]* %4, i64 0, i64 0
%31 = call i32 @find_prime_factors(i32 %27, i32* %28, i32 %29, i32* %30, i32* %5)
%32 = load i32, i32* %7
%33 = call i32 @sum_digits(i32 %32)
store i32 %33, i32* %8
store i32 0, i32* %9
store i32 0, i32* %10
br label %34
34: ; preds = %46, %26
%35 = load i32, i32* %10
%36 = load i32, i32* %5
%37 = icmp slt i32 %35, %36
br i1 %37, label %38, label %49
38: ; preds = %34
%39 = load i32, i32* %10
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds [5120 x i32], [5120 x i32]* %4, i64 0, i64 %40
%42 = load i32, i32* %41
%43 = call i32 @sum_digits(i32 %42)
%44 = load i32, i32* %9
%45 = add nsw i32 %44, %43
store i32 %45, i32* %9
br label %46
46: ; preds = %38
%47 = load i32, i32* %10
%48 = add nsw i32 %47, 1
store i32 %48, i32* %10
br label %34
49: ; preds = %34
%50 = load i32, i32* %5
%51 = icmp ne i32 %50, 1
br i1 %51, label %52, label %59
52: ; preds = %49
%53 = load i32, i32* %8
%54 = load i32, i32* %9
%55 = icmp eq i32 %53, %54
br i1 %55, label %56, label %59
56: ; preds = %52
%57 = load i32, i32* %7
%58 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %57)
br label %60
59: ; preds = %52, %49
br label %22
60: ; preds = %56, %22
br label %16
61: ; preds = %16
ret i32 0
}

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