#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 Caculate_len(int *, int *, int);
int /*<<< orphan*/ PrintEncode(int *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
int /*<<< orphan*/ scanf(char *, ...);
int main() {
  int p, i, j;
  FILE *f = fopen("a.out", "w");
  scanf("%d", &p);
  for (i = 0; i < p; i++) {
    int n, B;
    int read[20000 + 1], wt[20000 + 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++;
      }
    }
    j = Caculate_len(read, wt, B);
    fprintf(f, "%d %d\n", n, j / 2);
    PrintEncode(wt, j, f);
    fprintf(f, "\n");
  }
  fclose(f);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x27150,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe87(%rip),%rdi
lea    0xe86(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
lea    0xe7a(%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    1319 <main+0x1b9>
lea    0xe53(%rip),%rdi
lea    -0x1c(%rbp),%rsi
lea    -0x20(%rbp),%rdx
mov    $0x0,%al
call   1060 <scanf@plt>
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
mov    -0x20(%rbp),%ecx
shl    $0x1,%ecx
cmp    %ecx,%eax
jge    129f <main+0x13f>
lea    0xe2a(%rip),%rdi
lea    -0x27141(%rbp),%rsi
mov    $0x0,%al
call   1060 <scanf@plt>
movsbl -0x27141(%rbp),%eax
cmp    $0x39,%eax
jle    1211 <main+0xb1>
movsbl -0x27141(%rbp),%eax
cmp    $0x41,%eax
jl     1231 <main+0xd1>
movsbl -0x27141(%rbp),%eax
cmp    $0x30,%eax
jl     1231 <main+0xd1>
movsbl -0x27141(%rbp),%eax
cmp    $0x46,%eax
jle    1236 <main+0xd6>
jmp    11cb <main+0x6b>
movsbl -0x27141(%rbp),%eax
cmp    $0x30,%eax
jl     1279 <main+0x119>
movsbl -0x27141(%rbp),%eax
cmp    $0x39,%eax
jg     1279 <main+0x119>
movsbl -0x27141(%rbp),%ecx
sub    $0x30,%ecx
movslq -0x10(%rbp),%rax
mov    %ecx,-0x138b0(%rbp,%rax,4)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    129a <main+0x13a>
movsbl -0x27141(%rbp),%ecx
sub    $0x41,%ecx
add    $0xa,%ecx
movslq -0x10(%rbp),%rax
mov    %ecx,-0x138b0(%rbp,%rax,4)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    11cb <main+0x6b>
lea    -0x138b0(%rbp),%rdi
lea    -0x27140(%rbp),%rsi
mov    -0x20(%rbp),%edx
call   1040 <Caculate_len@plt>
mov    %eax,-0x10(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%eax
mov    %eax,-0x27148(%rbp)
mov    -0x10(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    -0x27148(%rbp),%edx
mov    %eax,%ecx
lea    0xd31(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
lea    -0x27140(%rbp),%rdi
mov    -0x10(%rbp),%esi
mov    -0x18(%rbp),%rdx
call   1080 <PrintEncode@plt>
mov    -0x18(%rbp),%rdi
lea    0xd11(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    11a2 <main+0x42>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
add    $0x27150,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x27128,%rsp
lea    0xe78(%rip),%rdi
lea    0xe77(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,%r13
lea    0xe6c(%rip),%rdi
lea    0x4(%rsp),%rsi
xor    %eax,%eax
call   1070 <scanf@plt>
cmpl   $0x0,0x4(%rsp)
jle    129b <main+0x12b>
lea    0xe51(%rip),%r15
lea    0x3(%rsp),%rbp
xor    %r12d,%r12d
jmp    122c <main+0xbc>
cs nopw 0x0(%rax,%rax,1)
lea    0x10(%rsp),%rdi
lea    0x138a0(%rsp),%rbx
mov    %rbx,%rsi
call   1040 <Caculate_len@plt>
mov    %eax,%r14d
mov    0xc(%rsp),%edx
mov    %eax,%ecx
shr    $0x1f,%ecx
add    %eax,%ecx
sar    %ecx
mov    %r13,%rdi
lea    0xe11(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    %rbx,%rdi
mov    %r14d,%esi
mov    %r13,%rdx
call   1090 <PrintEncode@plt>
mov    $0xa,%edi
mov    %r13,%rsi
call   1050 <fputc@plt>
add    $0x1,%r12d
cmp    0x4(%rsp),%r12d
jge    129b <main+0x12b>
lea    0xdd5(%rip),%rdi
lea    0xc(%rsp),%rsi
lea    0x8(%rsp),%rdx
xor    %eax,%eax
call   1070 <scanf@plt>
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
movslq 0x8(%rsp),%rdx
lea    (%rdx,%rdx,1),%rax
cmp    %rax,%rbx
jge    11d0 <main+0x60>
mov    %r15,%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1070 <scanf@plt>
movzbl 0x3(%rsp),%eax
lea    -0x47(%rax),%ecx
cmp    $0xe9,%cl
jb     1250 <main+0xe0>
lea    -0x3a(%rax),%ecx
cmp    $0x7,%cl
jb     1250 <main+0xe0>
lea    -0x37(%rax),%ecx
mov    %eax,%edx
add    $0xffffffd0,%edx
cmp    $0x3a,%al
cmovae %ecx,%edx
mov    %edx,0x10(%rsp,%rbx,4)
add    $0x1,%rbx
jmp    1250 <main+0xe0>
mov    %r13,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
add    $0x27128,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [6 x i8] c"a.out\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [3 x i8] c"%d\00"
@.str.3 = [5 x i8] c"%d%d\00"
@.str.4 = [3 x i8] c"%c\00"
@.str.5 = [7 x i8] c"%d %d\0A\00"
@.str.6 = [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 i32
%8 = alloca [20001 x i32]
%9 = alloca [20001 x i32]
%10 = alloca i8
store i32 0, i32* %1
%11 = call i32* @fopen(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %11, i32** %5
%12 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %2)
store i32 0, i32* %3
br label %13
13: ; preds = %86, %0
%14 = load i32, i32* %3
%15 = load i32, i32* %2
%16 = icmp slt i32 %14, %15
br i1 %16, label %17, label %89
17: ; preds = %13
%18 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32* %6, i32* %7)
store i32 0, i32* %4
br label %19
19: ; preds = %69, %41, %17
%20 = load i32, i32* %4
%21 = load i32, i32* %7
%22 = mul nsw i32 2, %21
%23 = icmp slt i32 %20, %22
br i1 %23, label %24, label %70
24: ; preds = %19
%25 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %10)
%26 = load i8, i8* %10
%27 = sext i8 %26 to i32
%28 = icmp sgt i32 %27, 57
br i1 %28, label %29, label %33
29: ; preds = %24
%30 = load i8, i8* %10
%31 = sext i8 %30 to i32
%32 = icmp slt i32 %31, 65
br i1 %32, label %41, label %33
33: ; preds = %29, %24
%34 = load i8, i8* %10
%35 = sext i8 %34 to i32
%36 = icmp slt i32 %35, 48
br i1 %36, label %41, label %37
37: ; preds = %33
%38 = load i8, i8* %10
%39 = sext i8 %38 to i32
%40 = icmp sgt i32 %39, 70
br i1 %40, label %41, label %42
41: ; preds = %37, %33, %29
br label %19
42: ; preds = %37
%43 = load i8, i8* %10
%44 = sext i8 %43 to i32
%45 = icmp sge i32 %44, 48
br i1 %45, label %46, label %59
46: ; preds = %42
%47 = load i8, i8* %10
%48 = sext i8 %47 to i32
%49 = icmp sle i32 %48, 57
br i1 %49, label %50, label %59
50: ; preds = %46
%51 = load i8, i8* %10
%52 = sext i8 %51 to i32
%53 = sub nsw i32 %52, 48
%54 = load i32, i32* %4
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds [20001 x i32], [20001 x i32]* %8, i64 0, i64 %55
store i32 %53, i32* %56
%57 = load i32, i32* %4
%58 = add nsw i32 %57, 1
store i32 %58, i32* %4
br label %69
59: ; preds = %46, %42
%60 = load i8, i8* %10
%61 = sext i8 %60 to i32
%62 = sub nsw i32 %61, 65
%63 = add nsw i32 %62, 10
%64 = load i32, i32* %4
%65 = sext i32 %64 to i64
%66 = getelementptr inbounds [20001 x i32], [20001 x i32]* %8, i64 0, i64 %65
store i32 %63, i32* %66
%67 = load i32, i32* %4
%68 = add nsw i32 %67, 1
store i32 %68, i32* %4
br label %69
69: ; preds = %59, %50
br label %19
70: ; preds = %19
%71 = getelementptr inbounds [20001 x i32], [20001 x i32]* %8, i64 0, i64 0
%72 = getelementptr inbounds [20001 x i32], [20001 x i32]* %9, i64 0, i64 0
%73 = load i32, i32* %7
%74 = call i32 @Caculate_len(i32* %71, i32* %72, i32 %73)
store i32 %74, i32* %4
%75 = load i32*, i32** %5
%76 = load i32, i32* %6
%77 = load i32, i32* %4
%78 = sdiv i32 %77, 2
%79 = call i32 (i32*, i8*, ...) @fprintf(i32* %75, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0), i32 %76, i32 %78)
%80 = getelementptr inbounds [20001 x i32], [20001 x i32]* %9, i64 0, i64 0
%81 = load i32, i32* %4
%82 = load i32*, i32** %5
%83 = call i32 @PrintEncode(i32* %80, i32 %81, i32* %82)
%84 = load i32*, i32** %5
%85 = call i32 (i32*, i8*, ...) @fprintf(i32* %84, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
br label %86
86: ; preds = %70
%87 = load i32, i32* %3
%88 = add nsw i32 %87, 1
store i32 %88, i32* %3
br label %13
89: ; preds = %13
%90 = load i32*, i32** %5
%91 = call i32 @fclose(i32* %90)
ret i32 0
}

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