#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 handle(int);
scalar_t__ is_num_pld(int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ scanf(char *, int *);
int main(int argc, const char *argv[]) {
  int n, a, i, j, flag;
  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    scanf("%d", &a);
    flag = 0;
    for (j = 1; j <= 8; j++) {
      a = handle(a);
      if (is_num_pld(a)) {
        printf("%d\n", j);
        flag = 1;
        break;
      }
    }
    if (!flag)
      printf("0\n");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xea3(%rip),%rdi
lea    -0x14(%rbp),%rsi
call   1060 <scanf@plt>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1219 <main+0xd9>
lea    0xe80(%rip),%rdi
lea    -0x18(%rbp),%rsi
call   1060 <scanf@plt>
movl   $0x0,-0x24(%rbp)
movl   $0x1,-0x20(%rbp)
cmpl   $0x8,-0x20(%rbp)
jg     11ee <main+0xae>
mov    -0x18(%rbp),%edi
call   1050 <handle@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%edi
call   1030 <is_num_pld@plt>
cmp    $0x0,%rax
je     11db <main+0x9b>
mov    -0x20(%rbp),%esi
lea    0xe3b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x24(%rbp)
jmp    11ee <main+0xae>
jmp    11e0 <main+0xa0>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1197 <main+0x57>
cmpl   $0x0,-0x24(%rbp)
jne    1206 <main+0xc6>
lea    0xe08(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    120b <main+0xcb>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    116d <main+0x2d>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
lea    0xe9d(%rip),%rdi
lea    0xc(%rsp),%rsi
call   1070 <scanf@plt>
cmpl   $0x0,0xc(%rsp)
jle    12d3 <main+0x183>
lea    0xe81(%rip),%r14
lea    0x8(%rsp),%r12
lea    0xe7c(%rip),%r15
lea    0xe71(%rip),%rbx
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r14,%rdi
mov    %r12,%rsi
call   1070 <scanf@plt>
mov    0x8(%rsp),%edi
call   1060 <handle@plt>
mov    %eax,0x8(%rsp)
mov    %eax,%edi
call   1040 <is_num_pld@plt>
mov    $0x1,%esi
test   %rax,%rax
jne    12b0 <main+0x160>
mov    0x8(%rsp),%edi
call   1060 <handle@plt>
mov    %eax,0x8(%rsp)
mov    %eax,%edi
call   1040 <is_num_pld@plt>
mov    $0x2,%esi
test   %rax,%rax
jne    12b0 <main+0x160>
mov    0x8(%rsp),%edi
call   1060 <handle@plt>
mov    %eax,0x8(%rsp)
mov    %eax,%edi
call   1040 <is_num_pld@plt>
mov    $0x3,%esi
test   %rax,%rax
jne    12b0 <main+0x160>
mov    0x8(%rsp),%edi
call   1060 <handle@plt>
mov    %eax,0x8(%rsp)
mov    %eax,%edi
call   1040 <is_num_pld@plt>
mov    $0x4,%esi
test   %rax,%rax
jne    12b0 <main+0x160>
mov    0x8(%rsp),%edi
call   1060 <handle@plt>
mov    %eax,0x8(%rsp)
mov    %eax,%edi
call   1040 <is_num_pld@plt>
mov    $0x5,%esi
test   %rax,%rax
jne    12b0 <main+0x160>
mov    0x8(%rsp),%edi
call   1060 <handle@plt>
mov    %eax,0x8(%rsp)
mov    %eax,%edi
call   1040 <is_num_pld@plt>
mov    $0x6,%esi
test   %rax,%rax
jne    12b0 <main+0x160>
mov    0x8(%rsp),%edi
call   1060 <handle@plt>
mov    %eax,0x8(%rsp)
mov    %eax,%edi
call   1040 <is_num_pld@plt>
mov    $0x7,%esi
test   %rax,%rax
jne    12b0 <main+0x160>
mov    0x8(%rsp),%edi
call   1060 <handle@plt>
mov    %eax,0x8(%rsp)
mov    %eax,%edi
call   1040 <is_num_pld@plt>
mov    $0x8,%esi
test   %rax,%rax
je     12c9 <main+0x179>
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
add    $0x1,%ebp
cmp    0xc(%rsp),%ebp
jl     11a0 <main+0x50>
jmp    12d3 <main+0x183>
mov    %r15,%rdi
call   1030 <puts@plt>
jmp    12ba <main+0x16a>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%d\00"
@.str.1 = [4 x i8] c"%d\0A\00"
@.str.2 = [3 x i8] c"0\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %6)
store i32 0, i32* %8
br label %12
12: ; preds = %40, %2
%13 = load i32, i32* %8
%14 = load i32, i32* %6
%15 = icmp slt i32 %13, %14
br i1 %15, label %16, label %43
16: ; preds = %12
%17 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %7)
store i32 0, i32* %10
store i32 1, i32* %9
br label %18
18: ; preds = %31, %16
%19 = load i32, i32* %9
%20 = icmp sle i32 %19, 8
br i1 %20, label %21, label %34
21: ; preds = %18
%22 = load i32, i32* %7
%23 = call i32 @handle(i32 %22)
store i32 %23, i32* %7
%24 = load i32, i32* %7
%25 = call i64 @is_num_pld(i32 %24)
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %21
%28 = load i32, i32* %9
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %28)
store i32 1, i32* %10
br label %34
30: ; preds = %21
br label %31
31: ; preds = %30
%32 = load i32, i32* %9
%33 = add nsw i32 %32, 1
store i32 %33, i32* %9
br label %18
34: ; preds = %27, %18
%35 = load i32, i32* %10
%36 = icmp ne i32 %35, 0
br i1 %36, label %39, label %37
37: ; preds = %34
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
br label %39
39: ; preds = %37, %34
br label %40
40: ; preds = %39
%41 = load i32, i32* %8
%42 = add nsw i32 %41, 1
store i32 %42, i32* %8
br label %12
43: ; preds = %12
ret i32 0
}

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