#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*/ atoi(char *);
int g1(int, char *, int);
int g2(int, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ puts(char *);
int /*<<< orphan*/ scanf(char *, int *, ...);
int strlen(char *);
int main() {
  int a, t, l;
  char b[260];
  scanf("%d", &t);
  while (t--) {
    scanf("%d %s\n", &a, b);
    l = strlen(b);
    if (!a)
      printf("%s\n", b);
    else if (a == 1)
      puts("1");
    else if (l < 10)
      printf("%d\n", g2(a, atoi(b)));
    else
      printf("%d\n", g1(a, b, l));
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe77(%rip),%rdi
lea    -0xc(%rbp),%rsi
mov    $0x0,%al
call   1080 <scanf@plt>
mov    -0xc(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0xc(%rbp)
cmp    $0x0,%eax
je     1281 <main+0x111>
lea    -0x120(%rbp),%rdx
lea    0xe4d(%rip),%rdi
lea    -0x8(%rbp),%rsi
mov    $0x0,%al
call   1080 <scanf@plt>
lea    -0x120(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x8(%rbp)
jne    11f4 <main+0x84>
lea    -0x120(%rbp),%rsi
lea    0xe1e(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    127c <main+0x10c>
cmpl   $0x1,-0x8(%rbp)
jne    120f <main+0x9f>
lea    0xe05(%rip),%rdi
call   1030 <puts@plt>
jmp    1277 <main+0x107>
cmpl   $0xa,-0x10(%rbp)
jge    1250 <main+0xe0>
mov    -0x8(%rbp),%eax
mov    %eax,-0x124(%rbp)
lea    -0x120(%rbp),%rdi
call   1090 <atoi@plt>
mov    -0x124(%rbp),%edi
mov    %eax,%esi
call   1070 <g2@plt>
mov    %eax,%esi
lea    0xdc8(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    1272 <main+0x102>
mov    -0x8(%rbp),%edi
lea    -0x120(%rbp),%rsi
mov    -0x10(%rbp),%edx
call   1040 <g1@plt>
mov    %eax,%esi
lea    0xda1(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    1277 <main+0x107>
jmp    127c <main+0x10c>
jmp    1194 <main+0x24>
xor    %eax,%eax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x118,%rsp
lea    0xe78(%rip),%rdi
lea    0x8(%rsp),%rsi
xor    %eax,%eax
call   1080 <scanf@plt>
subl   $0x1,0x8(%rsp)
jb     1237 <main+0xc7>
lea    0xe5d(%rip),%r14
lea    0xc(%rsp),%r15
lea    0x10(%rsp),%rbx
lea    0xe53(%rip),%r12
lea    0xe4e(%rip),%r13
jmp    11cf <main+0x5f>
mov    %r12,%rdi
call   1030 <puts@plt>
subl   $0x1,0x8(%rsp)
jb     1237 <main+0xc7>
mov    %r14,%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1080 <scanf@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    0xc(%rsp),%ebp
cmp    $0x1,%ebp
je     11c0 <main+0x50>
test   %ebp,%ebp
jne    1200 <main+0x90>
mov    %rbx,%rdi
jmp    11c3 <main+0x53>
nopl   0x0(%rax)
cmp    $0x9,%eax
jg     1218 <main+0xa8>
mov    %rbx,%rdi
call   1090 <atoi@plt>
mov    %ebp,%edi
mov    %eax,%esi
call   1070 <g2@plt>
jmp    1224 <main+0xb4>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <g1@plt>
mov    %r13,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
subl   $0x1,0x8(%rsp)
jae    11cf <main+0x5f>
xor    %eax,%eax
add    $0x118,%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 = [7 x i8] c"%d %s\0A\00"
@.str.2 = [4 x i8] c"%s\0A\00"
@.str.3 = [2 x i8] c"1\00"
@.str.4 = [4 x i8] c"%d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca [260 x i8]
store i32 0, i32* %1
%6 = call i32 (i8*, i32*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %3)
br label %7
7: ; preds = %43, %0
%8 = load i32, i32* %3
%9 = add nsw i32 %8, -1
store i32 %9, i32* %3
%10 = icmp ne i32 %8, 0
br i1 %10, label %11, label %44
11: ; preds = %7
%12 = getelementptr inbounds [260 x i8], [260 x i8]* %5, i64 0, i64 0
%13 = call i32 (i8*, i32*, ...) @scanf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32* %2, i8* %12)
%14 = getelementptr inbounds [260 x i8], [260 x i8]* %5, i64 0, i64 0
%15 = call i32 @strlen(i8* %14)
store i32 %15, i32* %4
%16 = load i32, i32* %2
%17 = icmp ne i32 %16, 0
br i1 %17, label %21, label %18
18: ; preds = %11
%19 = getelementptr inbounds [260 x i8], [260 x i8]* %5, i64 0, i64 0
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %19)
br label %43
21: ; preds = %11
%22 = load i32, i32* %2
%23 = icmp eq i32 %22, 1
br i1 %23, label %24, label %26
24: ; preds = %21
%25 = call i32 @puts(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
br label %42
26: ; preds = %21
%27 = load i32, i32* %4
%28 = icmp slt i32 %27, 10
br i1 %28, label %29, label %35
29: ; preds = %26
%30 = load i32, i32* %2
%31 = getelementptr inbounds [260 x i8], [260 x i8]* %5, i64 0, i64 0
%32 = call i32 @atoi(i8* %31)
%33 = call i32 @g2(i32 %30, i32 %32)
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 %33)
br label %41
35: ; preds = %26
%36 = load i32, i32* %2
%37 = getelementptr inbounds [260 x i8], [260 x i8]* %5, i64 0, i64 0
%38 = load i32, i32* %4
%39 = call i32 @g1(i32 %36, i8* %37, i32 %38)
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 %39)
br label %41
41: ; preds = %35, %29
br label %42
42: ; preds = %41, %24
br label %43
43: ; preds = %42, %18
br label %7
44: ; preds = %7
ret i32 0
}

LLVM IR (-O2):
@.str = [3 x i8] c"%d\00"
@.str.1 = [7 x i8] c"%d %s\0A\00"
@.str.3 = [2 x i8] c"1\00"
@.str.4 = [4 x i8] c"%d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [260 x i8]
%4 = bitcast i32* %1 to i8*
%5 = bitcast i32* %2 to i8*
%6 = getelementptr inbounds [260 x i8], [260 x i8]* %3, i64 0, i64 0
%7 = call i32 (i8*, i32*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %2)
%8 = load i32, i32* %2
%9 = add nsw i32 %8, -1
store i32 %9, i32* %2
%10 = icmp eq i32 %8, 0
br i1 %10, label %32, label %11
11: ; preds = %0, %28
%12 = call i32 (i8*, i32*, ...) @scanf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32* %1, i8* %6)
%13 = call i32 @strlen(i8* %6)
%14 = load i32, i32* %1
switch i32 %14, label %19 [
i32 0, label %15
i32 1, label %17
]
15: ; preds = %11
%16 = call i32 @puts(i8* %6)
br label %28
17: ; preds = %11
%18 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
br label %28
19: ; preds = %11
%20 = icmp slt i32 %13, 10
br i1 %20, label %21, label %25
21: ; preds = %19
%22 = call i32 @atoi(i8* %6)
%23 = call i32 @g2(i32 %14, i32 %22)
%24 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 %23)
br label %28
25: ; preds = %19
%26 = call i32 @g1(i32 %14, i8* %6, i32 %13)
%27 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 %26)
br label %28
28: ; preds = %17, %25, %21, %15
%29 = load i32, i32* %2
%30 = add nsw i32 %29, -1
store i32 %30, i32* %2
%31 = icmp eq i32 %29, 0
br i1 %31, label %32, label %11
32: ; preds = %28, %0
ret i32 0
}