#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*/ Caculate_len(int *, int);
int /*<<< orphan*/ PrintDecode(int *, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ scanf(char *, ...);
int main() {
  int p, i, j;
  scanf("%d", &p);
  for (i = 0; i < p; i++) {
    int n, B;
    int read[10000 + 1];
    char temp;
    scanf("%d%d", &n, &B);
    for (j = 0; j < 2 * B;) {
      scanf("%c", &temp);
      if ((temp > '9' && temp < 'A') || (temp < '0') || (temp > 'F')) {
        continue;
      }
      if (temp >= '0' && temp <= '9') {
        read[j] = temp - '0';
        j++;
      } else {
        read[j] = temp - 'A' + 10;
        j++;
      }
    }
    printf("%d %ld\n", n, Caculate_len(read, B));
    PrintDecode(read, B);
    printf("\n");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x9c70,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xea9(%rip),%rdi
lea    -0x8(%rbp),%rsi
mov    $0x0,%al
call   1060 <scanf@plt>
movl   $0x0,-0xc(%rbp)
mov    -0xc(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    12c1 <main+0x181>
lea    0xe82(%rip),%rdi
lea    -0x14(%rbp),%rsi
lea    -0x18(%rbp),%rdx
mov    $0x0,%al
call   1060 <scanf@plt>
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
mov    -0x18(%rbp),%ecx
shl    $0x1,%ecx
cmp    %ecx,%eax
jge    1268 <main+0x128>
lea    0xe59(%rip),%rdi
lea    -0x9c61(%rbp),%rsi
mov    $0x0,%al
call   1060 <scanf@plt>
movsbl -0x9c61(%rbp),%eax
cmp    $0x39,%eax
jle    11da <main+0x9a>
movsbl -0x9c61(%rbp),%eax
cmp    $0x41,%eax
jl     11fa <main+0xba>
movsbl -0x9c61(%rbp),%eax
cmp    $0x30,%eax
jl     11fa <main+0xba>
movsbl -0x9c61(%rbp),%eax
cmp    $0x46,%eax
jle    11ff <main+0xbf>
jmp    1194 <main+0x54>
movsbl -0x9c61(%rbp),%eax
cmp    $0x30,%eax
jl     1242 <main+0x102>
movsbl -0x9c61(%rbp),%eax
cmp    $0x39,%eax
jg     1242 <main+0x102>
movsbl -0x9c61(%rbp),%ecx
sub    $0x30,%ecx
movslq -0x10(%rbp),%rax
mov    %ecx,-0x9c60(%rbp,%rax,4)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1263 <main+0x123>
movsbl -0x9c61(%rbp),%ecx
sub    $0x41,%ecx
add    $0xa,%ecx
movslq -0x10(%rbp),%rax
mov    %ecx,-0x9c60(%rbp,%rax,4)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1194 <main+0x54>
mov    -0x14(%rbp),%eax
mov    %eax,-0x9c68(%rbp)
lea    -0x9c60(%rbp),%rdi
mov    -0x18(%rbp),%esi
call   1050 <Caculate_len@plt>
mov    -0x9c68(%rbp),%esi
mov    %eax,%edx
lea    0xd79(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x9c60(%rbp),%rdi
mov    -0x18(%rbp),%esi
call   1030 <PrintDecode@plt>
lea    0xd62(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    116b <main+0x2b>
xor    %eax,%eax
add    $0x9c70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x9c58,%rsp
lea    0xe9a(%rip),%rdi
lea    0x8(%rsp),%rsi
xor    %eax,%eax
call   1070 <scanf@plt>
cmpl   $0x0,0x8(%rsp)
jle    1247 <main+0xf7>
lea    0xe7f(%rip),%r13
lea    0x3(%rsp),%rbx
lea    0x10(%rsp),%r14
xor    %r12d,%r12d
jmp    11e1 <main+0x91>
cs nopw 0x0(%rax,%rax,1)
nop
mov    0xc(%rsp),%r15d
mov    %r14,%rdi
call   1060 <Caculate_len@plt>
lea    0xe54(%rip),%rdi
mov    %r15d,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x4(%rsp),%esi
mov    %r14,%rdi
call   1040 <PrintDecode@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
add    $0x1,%r12d
cmp    0x8(%rsp),%r12d
jge    1247 <main+0xf7>
lea    0xe18(%rip),%rdi
lea    0xc(%rsp),%rsi
lea    0x4(%rsp),%rdx
xor    %eax,%eax
call   1070 <scanf@plt>
xor    %ebp,%ebp
nopl   0x0(%rax,%rax,1)
movslq 0x4(%rsp),%rsi
lea    (%rsi,%rsi,1),%rax
cmp    %rax,%rbp
jge    11a0 <main+0x50>
mov    %r13,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1070 <scanf@plt>
movzbl 0x3(%rsp),%eax
lea    -0x47(%rax),%ecx
cmp    $0xe9,%cl
jb     1200 <main+0xb0>
lea    -0x3a(%rax),%ecx
cmp    $0x7,%cl
jb     1200 <main+0xb0>
lea    -0x37(%rax),%ecx
mov    %eax,%edx
add    $0xffffffd0,%edx
cmp    $0x3a,%al
cmovae %ecx,%edx
mov    %edx,0x10(%rsp,%rbp,4)
add    $0x1,%rbp
jmp    1200 <main+0xb0>
xor    %eax,%eax
add    $0x9c58,%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 = [5 x i8] c"%d%d\00"
@.str.2 = [3 x i8] c"%c\00"
@.str.3 = [8 x i8] c"%d %ld\0A\00"
@.str.4 = [2 x i8] c"\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 [10001 x i32]
%8 = alloca i8
store i32 0, i32* %1
%9 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %2)
store i32 0, i32* %3
br label %10
10: ; preds = %77, %0
%11 = load i32, i32* %3
%12 = load i32, i32* %2
%13 = icmp slt i32 %11, %12
br i1 %13, label %14, label %80
14: ; preds = %10
%15 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32* %5, i32* %6)
store i32 0, i32* %4
br label %16
16: ; preds = %66, %38, %14
%17 = load i32, i32* %4
%18 = load i32, i32* %6
%19 = mul nsw i32 2, %18
%20 = icmp slt i32 %17, %19
br i1 %20, label %21, label %67
21: ; preds = %16
%22 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %8)
%23 = load i8, i8* %8
%24 = sext i8 %23 to i32
%25 = icmp sgt i32 %24, 57
br i1 %25, label %26, label %30
26: ; preds = %21
%27 = load i8, i8* %8
%28 = sext i8 %27 to i32
%29 = icmp slt i32 %28, 65
br i1 %29, label %38, label %30
30: ; preds = %26, %21
%31 = load i8, i8* %8
%32 = sext i8 %31 to i32
%33 = icmp slt i32 %32, 48
br i1 %33, label %38, label %34
34: ; preds = %30
%35 = load i8, i8* %8
%36 = sext i8 %35 to i32
%37 = icmp sgt i32 %36, 70
br i1 %37, label %38, label %39
38: ; preds = %34, %30, %26
br label %16
39: ; preds = %34
%40 = load i8, i8* %8
%41 = sext i8 %40 to i32
%42 = icmp sge i32 %41, 48
br i1 %42, label %43, label %56
43: ; preds = %39
%44 = load i8, i8* %8
%45 = sext i8 %44 to i32
%46 = icmp sle i32 %45, 57
br i1 %46, label %47, label %56
47: ; preds = %43
%48 = load i8, i8* %8
%49 = sext i8 %48 to i32
%50 = sub nsw i32 %49, 48
%51 = load i32, i32* %4
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds [10001 x i32], [10001 x i32]* %7, i64 0, i64 %52
store i32 %50, i32* %53
%54 = load i32, i32* %4
%55 = add nsw i32 %54, 1
store i32 %55, i32* %4
br label %66
56: ; preds = %43, %39
%57 = load i8, i8* %8
%58 = sext i8 %57 to i32
%59 = sub nsw i32 %58, 65
%60 = add nsw i32 %59, 10
%61 = load i32, i32* %4
%62 = sext i32 %61 to i64
%63 = getelementptr inbounds [10001 x i32], [10001 x i32]* %7, i64 0, i64 %62
store i32 %60, i32* %63
%64 = load i32, i32* %4
%65 = add nsw i32 %64, 1
store i32 %65, i32* %4
br label %66
66: ; preds = %56, %47
br label %16
67: ; preds = %16
%68 = load i32, i32* %5
%69 = getelementptr inbounds [10001 x i32], [10001 x i32]* %7, i64 0, i64 0
%70 = load i32, i32* %6
%71 = call i32 @Caculate_len(i32* %69, i32 %70)
%72 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i32 %68, i32 %71)
%73 = getelementptr inbounds [10001 x i32], [10001 x i32]* %7, i64 0, i64 0
%74 = load i32, i32* %6
%75 = call i32 @PrintDecode(i32* %73, i32 %74)
%76 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
br label %77
77: ; preds = %67
%78 = load i32, i32* %3
%79 = add nsw i32 %78, 1
store i32 %79, i32* %3
br label %10
80: ; preds = %10
ret i32 0
}

LLVM IR (-O2):
@.str = [3 x i8] c"%d\00"
@.str.1 = [5 x i8] c"%d%d\00"
@.str.2 = [3 x i8] c"%c\00"
@.str.3 = [8 x i8] c"%d %ld\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca [10001 x i32]
%5 = alloca i8
%6 = bitcast i32* %1 to i8*
%7 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %1)
%8 = load i32, i32* %1
%9 = icmp sgt i32 %8, 0
br i1 %9, label %10, label %50
10: ; preds = %0
%11 = bitcast i32* %2 to i8*
%12 = bitcast i32* %3 to i8*
%13 = bitcast [10001 x i32]* %4 to i8*
%14 = getelementptr inbounds [10001 x i32], [10001 x i32]* %4, i64 0, i64 0
br label %15
15: ; preds = %10, %40
%16 = phi i32 [ 0, %10 ], [ %47, %40 ]
%17 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32* %2, i32* %3)
br label %18
18: ; preds = %33, %15
%19 = phi i64 [ %39, %33 ], [ 0, %15 ]
br label %20
20: ; preds = %18, %25
%21 = load i32, i32* %3
%22 = shl nsw i32 %21, 1
%23 = sext i32 %22 to i64
%24 = icmp slt i64 %19, %23
br i1 %24, label %25, label %40
25: ; preds = %20
%26 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %5)
%27 = load i8, i8* %5
%28 = add i8 %27, -58
%29 = icmp ult i8 %28, 7
%30 = add i8 %27, -71
%31 = icmp ult i8 %30, -23
%32 = or i1 %31, %29
br i1 %32, label %20, label %33
33: ; preds = %25
%34 = zext i8 %27 to i32
%35 = icmp ult i8 %27, 58
%36 = select i1 %35, i32 -48, i32 -55
%37 = add nsw i32 %36, %34
%38 = getelementptr inbounds [10001 x i32], [10001 x i32]* %4, i64 0, i64 %19
store i32 %37, i32* %38
%39 = add nuw nsw i64 %19, 1
br label %18
40: ; preds = %20
%41 = load i32, i32* %2
%42 = call i32 @Caculate_len(i32* %14, i32 %21)
%43 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i32 %41, i32 %42)
%44 = load i32, i32* %3
%45 = call i32 @PrintDecode(i32* %14, i32 %44)
%46 = call i32 @putchar(i32 10)
%47 = add nuw nsw i32 %16, 1
%48 = load i32, i32* %1
%49 = icmp slt i32 %47, %48
br i1 %49, label %15, label %50
50: ; preds = %40, %0
ret i32 0
}