#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*/ printf(char *, int);
int /*<<< orphan*/ scanf(char *, ...);
int strlen(char *);
scalar_t__ strncmp(char *, char *, int);
int main() {
  char Alfa[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ", String0[51];
  int Cont, N, NLin, Cont2, Lim, Cont3, Cont4, Som = 0, Cond = 0;
  scanf("%d", &N);
  for (Cont = 1; Cont <= N; Cont++) {
    scanf("%d", &NLin);
    Som = 0;
    for (Cont2 = 0; Cont2 < NLin; Cont2++) {
      scanf("%s", String0);
      Lim = strlen(String0) - 1;
      Cont4 = 0;
      for (Cont3 = 0; Cont3 <= Lim; Cont3++) {
        Cond = 0;
        while (Cond != 1) {
          if (strncmp(&String0[Cont3], &Alfa[Cont4], 1) == 0) {
            Som = Som + Cont4;
            Som = Som + Cont3;
            Som = Som + Cont2;
            Cond = 1;
            Cont4 = 0;
          } else
            Cont4++;
        }
      }
    }
    printf("%d\n", Som);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
movl   $0x0,-0x4(%rbp)
mov    0xea7(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    0xea4(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    0xea1(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    0xe9e(%rip),%ax
mov    %ax,-0x8(%rbp)
mov    0xe96(%rip),%al
mov    %al,-0x6(%rbp)
movl   $0x0,-0x80(%rbp)
movl   $0x0,-0x84(%rbp)
lea    0xe7c(%rip),%rdi
lea    -0x68(%rbp),%rsi
mov    $0x0,%al
call   1060 <scanf@plt>
movl   $0x1,-0x64(%rbp)
mov    -0x64(%rbp),%eax
cmp    -0x68(%rbp),%eax
jg     12e7 <main+0x1a7>
lea    0xe57(%rip),%rdi
lea    -0x6c(%rbp),%rsi
mov    $0x0,%al
call   1060 <scanf@plt>
movl   $0x0,-0x80(%rbp)
movl   $0x0,-0x70(%rbp)
mov    -0x70(%rbp),%eax
cmp    -0x6c(%rbp),%eax
jge    12ca <main+0x18a>
lea    -0x60(%rbp),%rsi
lea    0xe2a(%rip),%rdi
mov    $0x0,%al
call   1060 <scanf@plt>
lea    -0x60(%rbp),%rdi
call   1040 <strlen@plt>
sub    $0x1,%eax
mov    %eax,-0x74(%rbp)
movl   $0x0,-0x7c(%rbp)
movl   $0x0,-0x78(%rbp)
mov    -0x78(%rbp),%eax
cmp    -0x74(%rbp),%eax
jg     12b7 <main+0x177>
movl   $0x0,-0x84(%rbp)
cmpl   $0x1,-0x84(%rbp)
je     12a4 <main+0x164>
movslq -0x78(%rbp),%rax
lea    -0x60(%rbp),%rdi
add    %rax,%rdi
movslq -0x7c(%rbp),%rax
lea    -0x20(%rbp),%rsi
add    %rax,%rsi
mov    $0x1,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    1296 <main+0x156>
mov    -0x80(%rbp),%eax
add    -0x7c(%rbp),%eax
mov    %eax,-0x80(%rbp)
mov    -0x80(%rbp),%eax
add    -0x78(%rbp),%eax
mov    %eax,-0x80(%rbp)
mov    -0x80(%rbp),%eax
add    -0x70(%rbp),%eax
mov    %eax,-0x80(%rbp)
movl   $0x1,-0x84(%rbp)
movl   $0x0,-0x7c(%rbp)
jmp    129f <main+0x15f>
mov    -0x7c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x7c(%rbp)
jmp    122e <main+0xee>
jmp    12a9 <main+0x169>
mov    -0x78(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x78(%rbp)
jmp    1218 <main+0xd8>
jmp    12bc <main+0x17c>
mov    -0x70(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x70(%rbp)
jmp    11dd <main+0x9d>
mov    -0x80(%rbp),%esi
lea    0xd4d(%rip),%rdi
call   1050 <printf@plt>
mov    -0x64(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x64(%rbp)
jmp    11b1 <main+0x71>
xor    %eax,%eax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x88,%rsp
movups 0xeb3(%rip),%xmm0
movups %xmm0,0x3b(%rsp)
movaps 0xe9c(%rip),%xmm0
movaps %xmm0,0x30(%rsp)
lea    0xeab(%rip),%rdi
lea    0x10(%rsp),%rsi
xor    %eax,%eax
call   1060 <scanf@plt>
cmpl   $0x0,0x10(%rsp)
jle    12b3 <main+0x173>
mov    $0x1,%eax
jmp    11b0 <main+0x70>
xchg   %ax,%ax
lea    0xe8a(%rip),%rdi
mov    %ebp,%esi
call   1050 <printf@plt>
mov    0x18(%rsp),%rcx
lea    0x1(%rcx),%eax
cmp    0x10(%rsp),%ecx
jge    12b3 <main+0x173>
mov    %rax,0x18(%rsp)
lea    0xe5f(%rip),%rdi
lea    0xc(%rsp),%rsi
xor    %eax,%eax
call   1060 <scanf@plt>
cmpl   $0x0,0xc(%rsp)
mov    $0x0,%ebp
jle    1190 <main+0x50>
xor    %r14d,%r14d
mov    $0xffffffff,%r15d
xor    %ebp,%ebp
jmp    120c <main+0xcc>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    0x20(%rsp),%r14
add    $0x1,%r14
movslq 0xc(%rsp),%rax
mov    0x14(%rsp),%r15d
add    $0x1,%r15d
cmp    %rax,%r14
jge    1190 <main+0x50>
lea    0xe0b(%rip),%rdi
lea    0x50(%rsp),%rbx
mov    %rbx,%rsi
xor    %eax,%eax
call   1060 <scanf@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
test   %eax,%eax
mov    %r14,0x20(%rsp)
mov    %r15d,0x14(%rsp)
jle    11f0 <main+0xb0>
mov    %eax,%eax
mov    %rax,0x28(%rsp)
mov    %r15d,%ebx
xor    %r15d,%r15d
xor    %r13d,%r13d
jmp    125e <main+0x11e>
nopw   0x0(%rax,%rax,1)
add    $0x1,%r15
add    $0x1,%ebx
cmp    0x28(%rsp),%r15
je     11f0 <main+0xb0>
lea    (%rsp,%r15,1),%r14
add    $0x50,%r14
xor    %ecx,%ecx
movslq %r13d,%r13
lea    (%rbx,%rbp,1),%eax
lea    (%rax,%r13,1),%r12d
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
test   $0x1,%cl
jne    1250 <main+0x110>
lea    (%rsp,%r13,1),%rsi
add    $0x30,%rsi
mov    %r14,%rdi
mov    $0x1,%edx
call   1030 <strncmp@plt>
add    $0x1,%r13
add    $0x1,%r12d
xor    %ecx,%ecx
test   %rax,%rax
jne    1280 <main+0x140>
mov    $0x1,%cl
xor    %r13d,%r13d
mov    %r12d,%ebp
jmp    1268 <main+0x128>
xor    %eax,%eax
add    $0x88,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@__const.main.Alfa = [27 x i8] c"ABCDEFGHIJKLMNOPQRSTUVWXYZ\00"
@.str = [3 x i8] c"%d\00"
@.str.1 = [3 x i8] c"%s\00"
@.str.2 = [4 x i8] c"%d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [27 x i8]
%3 = alloca [51 x i8]
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i32 0, i32* %1
%13 = bitcast [27 x i8]* %2 to i8*
store i32 0, i32* %11
store i32 0, i32* %12
%14 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %5)
store i32 1, i32* %4
br label %15
15: ; preds = %73, %0
%16 = load i32, i32* %4
%17 = load i32, i32* %5
%18 = icmp sle i32 %16, %17
br i1 %18, label %19, label %76
19: ; preds = %15
%20 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %6)
store i32 0, i32* %11
store i32 0, i32* %7
br label %21
21: ; preds = %67, %19
%22 = load i32, i32* %7
%23 = load i32, i32* %6
%24 = icmp slt i32 %22, %23
br i1 %24, label %25, label %70
25: ; preds = %21
%26 = getelementptr inbounds [51 x i8], [51 x i8]* %3, i64 0, i64 0
%27 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %26)
%28 = getelementptr inbounds [51 x i8], [51 x i8]* %3, i64 0, i64 0
%29 = call i32 @strlen(i8* %28)
%30 = sub nsw i32 %29, 1
store i32 %30, i32* %8
store i32 0, i32* %10
store i32 0, i32* %9
br label %31
31: ; preds = %63, %25
%32 = load i32, i32* %9
%33 = load i32, i32* %8
%34 = icmp sle i32 %32, %33
br i1 %34, label %35, label %66
35: ; preds = %31
store i32 0, i32* %12
br label %36
36: ; preds = %61, %35
%37 = load i32, i32* %12
%38 = icmp ne i32 %37, 1
br i1 %38, label %39, label %62
39: ; preds = %36
%40 = load i32, i32* %9
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds [51 x i8], [51 x i8]* %3, i64 0, i64 %41
%43 = load i32, i32* %10
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds [27 x i8], [27 x i8]* %2, i64 0, i64 %44
%46 = call i64 @strncmp(i8* %42, i8* %45, i32 1)
%47 = icmp eq i64 %46, 0
br i1 %47, label %48, label %58
48: ; preds = %39
%49 = load i32, i32* %11
%50 = load i32, i32* %10
%51 = add nsw i32 %49, %50
store i32 %51, i32* %11
%52 = load i32, i32* %11
%53 = load i32, i32* %9
%54 = add nsw i32 %52, %53
store i32 %54, i32* %11
%55 = load i32, i32* %11
%56 = load i32, i32* %7
%57 = add nsw i32 %55, %56
store i32 %57, i32* %11
store i32 1, i32* %12
store i32 0, i32* %10
br label %61
58: ; preds = %39
%59 = load i32, i32* %10
%60 = add nsw i32 %59, 1
store i32 %60, i32* %10
br label %61
61: ; preds = %58, %48
br label %36
62: ; preds = %36
br label %63
63: ; preds = %62
%64 = load i32, i32* %9
%65 = add nsw i32 %64, 1
store i32 %65, i32* %9
br label %31
66: ; preds = %31
br label %67
67: ; preds = %66
%68 = load i32, i32* %7
%69 = add nsw i32 %68, 1
store i32 %69, i32* %7
br label %21
70: ; preds = %21
%71 = load i32, i32* %11
%72 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %71)
br label %73
73: ; preds = %70
%74 = load i32, i32* %4
%75 = add nsw i32 %74, 1
store i32 %75, i32* %4
br label %15
76: ; preds = %15
ret i32 0
}

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