#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 */
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ FLineStdin();
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, int, long long);
int isTidy(long long, scalar_t__);
long long largestNum(scalar_t__);
scalar_t__ numDigits(long long);
int /*<<< orphan*/ printf(char *, long long);
long long smallestTidy(scalar_t__);
long long strtoi(int /*<<< orphan*/);
int main(void) {
  int iter = (int)strtoi(FLineStdin());
  int Case;
  FILE *fWrite = fopen("tidyNumbers.out", "w");
  for (Case = 0; Case < iter; Case++) {
    long long int num = strtoi(FLineStdin());
    long long int i = 0;
    if (num > smallestTidy(numDigits(num))) {
    } else {
      num = largestNum(numDigits(num) - 1);
    }
    while (isTidy(num - i, numDigits(num - i)) != 1) {
      i++;
    }
    printf("%lld\n", num - i);
    fprintf(fWrite, "Case #%d: %lld\n", Case + 1, num - i);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1030 <FLineStdin@plt>
mov    %eax,%edi
call   1080 <strtoi@plt>
mov    %eax,-0x8(%rbp)
lea    0xe49(%rip),%rdi
lea    0xe52(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x18(%rbp)
movl   $0x0,-0xc(%rbp)
mov    -0xc(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    12bf <main+0x12f>
mov    $0x0,%al
call   1030 <FLineStdin@plt>
mov    %eax,%edi
call   1080 <strtoi@plt>
mov    %rax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
call   10a0 <numDigits@plt>
mov    %rax,%rdi
call   1040 <smallestTidy@plt>
mov    %rax,%rcx
mov    -0x30(%rbp),%rax
cmp    %rcx,%rax
jle    1222 <main+0x92>
jmp    123b <main+0xab>
mov    -0x20(%rbp),%rdi
call   10a0 <numDigits@plt>
mov    %rax,%rdi
sub    $0x1,%rdi
call   1060 <largestNum@plt>
mov    %rax,-0x20(%rbp)
jmp    1240 <main+0xb0>
mov    -0x20(%rbp),%rax
sub    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%rdi
sub    -0x28(%rbp),%rdi
call   10a0 <numDigits@plt>
mov    -0x38(%rbp),%rdi
mov    %rax,%rsi
call   1050 <isTidy@plt>
cmp    $0x1,%eax
je     127f <main+0xef>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    1240 <main+0xb0>
mov    -0x20(%rbp),%rsi
sub    -0x28(%rbp),%rsi
lea    0xd8e(%rip),%rdi
call   1070 <printf@plt>
mov    -0x18(%rbp),%rdi
mov    -0xc(%rbp),%edx
add    $0x1,%edx
mov    -0x20(%rbp),%rcx
sub    -0x28(%rbp),%rcx
lea    0xd66(%rip),%rsi
call   1090 <fprintf@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    11ce <main+0x3e>
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
xor    %eax,%eax
call   1030 <FLineStdin@plt>
mov    %eax,%edi
call   1080 <strtoi@plt>
mov    %rax,%r14
lea    0xe4d(%rip),%rdi
lea    0xe56(%rip),%rsi
call   10b0 <fopen@plt>
test   %r14d,%r14d
jle    1267 <main+0xd7>
mov    %rax,%r13
lea    0xe4a(%rip),%r15
lea    0xe39(%rip),%r12
xor    %ebp,%ebp
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1030 <FLineStdin@plt>
mov    %eax,%edi
call   1080 <strtoi@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   10a0 <numDigits@plt>
mov    %rax,%rdi
call   1040 <smallestTidy@plt>
cmp    %rax,%rbx
jg     1220 <main+0x90>
mov    %rbx,%rdi
call   10a0 <numDigits@plt>
lea    -0x1(%rax),%rdi
call   1060 <largestNum@plt>
mov    %rax,%rbx
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   10a0 <numDigits@plt>
mov    %rbx,%rdi
mov    %rax,%rsi
call   1050 <isTidy@plt>
add    $0xffffffffffffffff,%rbx
cmp    $0x1,%eax
jne    1220 <main+0x90>
add    $0x1,%rbx
mov    %r15,%rdi
mov    %rbx,%rsi
call   1070 <printf@plt>
add    $0x1,%ebp
mov    %r13,%rdi
mov    %r12,%rsi
mov    %ebp,%edx
mov    %rbx,%rcx
call   1090 <fprintf@plt>
cmp    %r14d,%ebp
jne    11e0 <main+0x50>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [16 x i8] c"tidyNumbers.out\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [6 x i8] c"%lld\0A\00"
@.str.3 = [16 x i8] c"Case #%d: %lld\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i64
%6 = alloca i64
store i32 0, i32* %1
%7 = call i32 (...) @FLineStdin()
%8 = call i64 @strtoi(i32 %7)
%9 = trunc i64 %8 to i32
store i32 %9, i32* %2
%10 = call i32* @fopen(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %10, i32** %4
store i32 0, i32* %3
br label %11
11: ; preds = %55, %0
%12 = load i32, i32* %3
%13 = load i32, i32* %2
%14 = icmp slt i32 %12, %13
br i1 %14, label %15, label %58
15: ; preds = %11
%16 = call i32 (...) @FLineStdin()
%17 = call i64 @strtoi(i32 %16)
store i64 %17, i64* %5
store i64 0, i64* %6
%18 = load i64, i64* %5
%19 = load i64, i64* %5
%20 = call i64 @numDigits(i64 %19)
%21 = call i64 @smallestTidy(i64 %20)
%22 = icmp sgt i64 %18, %21
br i1 %22, label %23, label %24
23: ; preds = %15
br label %29
24: ; preds = %15
%25 = load i64, i64* %5
%26 = call i64 @numDigits(i64 %25)
%27 = sub nsw i64 %26, 1
%28 = call i64 @largestNum(i64 %27)
store i64 %28, i64* %5
br label %29
29: ; preds = %24, %23
br label %30
30: ; preds = %40, %29
%31 = load i64, i64* %5
%32 = load i64, i64* %6
%33 = sub nsw i64 %31, %32
%34 = load i64, i64* %5
%35 = load i64, i64* %6
%36 = sub nsw i64 %34, %35
%37 = call i64 @numDigits(i64 %36)
%38 = call i32 @isTidy(i64 %33, i64 %37)
%39 = icmp ne i32 %38, 1
br i1 %39, label %40, label %43
40: ; preds = %30
%41 = load i64, i64* %6
%42 = add nsw i64 %41, 1
store i64 %42, i64* %6
br label %30
43: ; preds = %30
%44 = load i64, i64* %5
%45 = load i64, i64* %6
%46 = sub nsw i64 %44, %45
%47 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i64 %46)
%48 = load i32*, i32** %4
%49 = load i32, i32* %3
%50 = add nsw i32 %49, 1
%51 = load i64, i64* %5
%52 = load i64, i64* %6
%53 = sub nsw i64 %51, %52
%54 = call i32 @fprintf(i32* %48, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i32 %50, i64 %53)
br label %55
55: ; preds = %43
%56 = load i32, i32* %3
%57 = add nsw i32 %56, 1
store i32 %57, i32* %3
br label %11
58: ; preds = %11
%59 = load i32, i32* %1
ret i32 %59
}

LLVM IR (-O2):
@.str = [16 x i8] c"tidyNumbers.out\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [6 x i8] c"%lld\0A\00"
@.str.3 = [16 x i8] c"Case #%d: %lld\0A\00"
define i32 @main() {
%1 = tail call i32 (...) @FLineStdin()
%2 = tail call i64 @strtoi(i32 %1)
%3 = trunc i64 %2 to i32
%4 = tail call i32* @fopen(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%5 = icmp sgt i32 %3, 0
br i1 %5, label %6, label %31
6: ; preds = %0, %26
%7 = phi i32 [ %28, %26 ], [ 0, %0 ]
%8 = tail call i32 (...) @FLineStdin()
%9 = tail call i64 @strtoi(i32 %8)
%10 = tail call i64 @numDigits(i64 %9)
%11 = tail call i64 @smallestTidy(i64 %10)
%12 = icmp sgt i64 %9, %11
br i1 %12, label %17, label %13
13: ; preds = %6
%14 = tail call i64 @numDigits(i64 %9)
%15 = add nsw i64 %14, -1
%16 = tail call i64 @largestNum(i64 %15)
br label %17
17: ; preds = %6, %13
%18 = phi i64 [ %9, %6 ], [ %16, %13 ]
br label %19
19: ; preds = %19, %17
%20 = phi i64 [ 0, %17 ], [ %25, %19 ]
%21 = sub nsw i64 %18, %20
%22 = tail call i64 @numDigits(i64 %21)
%23 = tail call i32 @isTidy(i64 %21, i64 %22)
%24 = icmp eq i32 %23, 1
%25 = add nuw nsw i64 %20, 1
br i1 %24, label %26, label %19
26: ; preds = %19
%27 = tail call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i64 %21)
%28 = add nuw nsw i32 %7, 1
%29 = tail call i32 @fprintf(i32* %4, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i32 %28, i64 %21)
%30 = icmp eq i32 %28, %3
br i1 %30, label %31, label %6
31: ; preds = %26, %0
ret i32 0
}