#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*/ printf(char *, int);
int /*<<< orphan*/ scanf(char *, int *);
int main() {
  int N, T;
  scanf("%d", &T);
  while (T--) {
    int total;
    int i;
    int status = 0;
    int mirror = 0, a = 0, b = 0, c = 0;
    scanf("%d", &N);
    total = N;
    while (status == 0) {
      i = 100;
      while (i <= 999) {
        if ((total % i) == 0 && (total / i) <= 999) {
          c = ((total % 1000) % 10) * 100;
          b = (((total % 1000) / 10) % 10) * 10;
          a = (total % 1000) / 100;
          mirror = c + b + a;
          if ((total / 1000) == mirror) {
            status = 1;
          }
          break;
        } else
          i++;
      }
      if (status == 0)
        total--;
    }
    printf("%d\n", total);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xeca(%rip),%rdi
lea    -0xc(%rbp),%rsi
call   1040 <scanf@plt>
mov    -0xc(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0xc(%rbp)
cmp    $0x0,%eax
je     1288 <main+0x168>
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
lea    0xe83(%rip),%rdi
lea    -0x8(%rbp),%rsi
call   1040 <scanf@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x18(%rbp)
jne    1274 <main+0x154>
movl   $0x64,-0x14(%rbp)
cmpl   $0x3e7,-0x14(%rbp)
jg     125c <main+0x13c>
mov    -0x10(%rbp),%eax
cltd
idivl  -0x14(%rbp)
cmp    $0x0,%edx
jne    124e <main+0x12e>
mov    -0x10(%rbp),%eax
cltd
idivl  -0x14(%rbp)
cmp    $0x3e7,%eax
jg     124e <main+0x12e>
mov    -0x10(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %edx,%eax
mov    $0xa,%ecx
cltd
idiv   %ecx
imul   $0x64,%edx,%eax
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %edx,%eax
mov    $0xa,%ecx
cltd
idiv   %ecx
mov    $0xa,%ecx
cltd
idiv   %ecx
imul   $0xa,%edx,%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %edx,%eax
mov    $0x64,%ecx
cltd
idiv   %ecx
mov    %eax,-0x20(%rbp)
mov    -0x28(%rbp),%eax
add    -0x24(%rbp),%eax
add    -0x20(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
cmp    -0x1c(%rbp),%eax
jne    1249 <main+0x129>
movl   $0x1,-0x18(%rbp)
jmp    125c <main+0x13c>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    119d <main+0x7d>
cmpl   $0x0,-0x18(%rbp)
jne    126f <main+0x14f>
mov    -0x10(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x10(%rbp)
jmp    118c <main+0x6c>
mov    -0x10(%rbp),%esi
lea    0xd85(%rip),%rdi
call   1030 <printf@plt>
jmp    113f <main+0x1f>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
lea    0xed1(%rip),%rdi
mov    %rsp,%rsi
call   1040 <scanf@plt>
subl   $0x1,(%rsp)
jae    114b <main+0x2b>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xeae(%rip),%r14
lea    0x4(%rsp),%r15
lea    0xea5(%rip),%r12
jmp    116e <main+0x4e>
mov    %r12,%rdi
call   1030 <printf@plt>
subl   $0x1,(%rsp)
jb     113d <main+0x1d>
mov    %r14,%rdi
mov    %r15,%rsi
call   1040 <scanf@plt>
mov    0x4(%rsp),%esi
jmp    121b <main+0xfb>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movslq %esi,%rax
imul   $0x10624dd3,%rax,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
sar    $0x26,%rax
add    %ecx,%eax
imul   $0x3e8,%eax,%ecx
mov    %esi,%edi
sub    %ecx,%edi
movswl %di,%ecx
imul   $0x6667,%ecx,%edx
mov    %edx,%ebx
shr    $0x1f,%ebx
sar    $0x12,%edx
add    %ebx,%edx
lea    (%rdx,%rdx,1),%ebx
lea    (%rbx,%rbx,4),%ebx
sub    %ebx,%edi
imul   $0x64,%edi,%r8d
movsbl %dl,%ebx
imul   $0x67,%ebx,%ebx
movzwl %bx,%ebx
movswl %bx,%edi
shr    $0xf,%ebx
shr    $0xa,%edi
add    %bl,%dil
movzbl %dil,%edi
add    %edi,%edi
lea    (%rdi,%rdi,4),%edi
sub    %dil,%dl
movsbl %dl,%edx
lea    (%rdx,%rdx,4),%edx
imul   $0x147b,%ecx,%ecx
mov    %ecx,%edi
shr    $0x1f,%edi
sar    $0x13,%ecx
add    %edi,%ecx
add    %r8d,%ecx
movswl %cx,%ecx
lea    (%rcx,%rdx,2),%ecx
cmp    %ecx,%eax
je     1160 <main+0x40>
add    $0xffffffff,%esi
mov    $0x64,%ecx
jmp    123b <main+0x11b>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
add    $0x1,%ecx
cmp    $0x3e8,%ecx
je     1218 <main+0xf8>
mov    %esi,%eax
cltd
idiv   %ecx
test   %edx,%edx
jne    124f <main+0x12f>
cmp    $0x3e8,%eax
jl     1190 <main+0x70>
add    $0x1,%ecx
mov    %esi,%eax
cltd
idiv   %ecx
test   %edx,%edx
jne    1230 <main+0x110>
cmp    $0x3e8,%eax
jge    1230 <main+0x110>
jmp    1190 <main+0x70>

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

LLVM IR (-O2):
@.str = [3 x i8] c"%d\00"
@.str.1 = [4 x i8] c"%d\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* %2)
%6 = load i32, i32* %2
%7 = add nsw i32 %6, -1
store i32 %7, i32* %2
%8 = icmp eq i32 %6, 0
br i1 %8, label %54, label %9
9: ; preds = %0, %49
%10 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %1)
%11 = load i32, i32* %1
br label %12
12: ; preds = %47, %9
%13 = phi i32 [ %11, %9 ], [ %48, %47 ]
br label %14
14: ; preds = %28, %12
%15 = phi i32 [ 100, %12 ], [ %29, %28 ]
%16 = srem i32 %13, %15
%17 = sdiv i32 %13, %15
%18 = icmp eq i32 %16, 0
%19 = icmp slt i32 %17, 1000
%20 = and i1 %18, %19
br i1 %20, label %31, label %21
21: ; preds = %14
%22 = or i32 %15, 1
%23 = srem i32 %13, %22
%24 = sdiv i32 %13, %22
%25 = icmp eq i32 %23, 0
%26 = icmp slt i32 %24, 1000
%27 = and i1 %25, %26
br i1 %27, label %31, label %28
28: ; preds = %21
%29 = add nuw nsw i32 %15, 2
%30 = icmp eq i32 %29, 1000
br i1 %30, label %47, label %14
31: ; preds = %21, %14
%32 = srem i32 %13, 1000
%33 = trunc i32 %32 to i16
%34 = srem i16 %33, 10
%35 = mul nsw i16 %34, 100
%36 = sdiv i16 %33, 10
%37 = trunc i16 %36 to i8
%38 = srem i8 %37, 10
%39 = sext i8 %38 to i32
%40 = mul nsw i32 %39, 10
%41 = sdiv i16 %33, 100
%42 = add nsw i16 %35, %41
%43 = sext i16 %42 to i32
%44 = add nsw i32 %40, %43
%45 = sdiv i32 %13, 1000
%46 = icmp eq i32 %45, %44
br i1 %46, label %49, label %47
47: ; preds = %28, %31
%48 = add nsw i32 %13, -1
br label %12
49: ; preds = %31
%50 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %13)
%51 = load i32, i32* %2
%52 = add nsw i32 %51, -1
store i32 %52, i32* %2
%53 = icmp eq i32 %51, 0
br i1 %53, label %54, label %9
54: ; preds = %49, %0
ret i32 0
}