int main() {
  int n, a0, i;
  int *cost;
  scanf("%d", &n);
  cost = (int *)malloc(n * sizeof(int));
  for (a0 = 0; a0 < n; a0++) {
    char *s = (char *)malloc((10 * 10 * 10 * 10 * 10 + 1) * sizeof(char));
    char *p = (char *)malloc((10 * 10 * 10 * 10 * 10 + 1) * sizeof(char));
    scanf("%s", s);
    cost[a0] = cost_for_substring(s, p);
    free(s);
    free(p);
  }
  for (a0 = 0; a0 < n; a0++) {
    printf("%d \n", cost[a0]);
  }
  free(cost);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe9a(%rip),%rdi
lea    -0x8(%rbp),%rsi
mov    $0x0,%al
call   1070 <scanf@plt>
movslq -0x8(%rbp),%rdi
shl    $0x2,%rdi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
movl   $0x0,-0xc(%rbp)
mov    -0xc(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    11ff <main+0xaf>
mov    $0x186a1,%edi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    $0x186a1,%edi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rsi
lea    0xe47(%rip),%rdi
mov    $0x0,%al
call   1070 <scanf@plt>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    $0x0,%al
call   1040 <cost_for_substring@plt>
mov    %eax,%edx
mov    -0x18(%rbp),%rax
movslq -0xc(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1189 <main+0x39>
movl   $0x0,-0xc(%rbp)
mov    -0xc(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    1239 <main+0xe9>
mov    -0x18(%rbp),%rax
movslq -0xc(%rbp),%rcx
mov    (%rax,%rcx,4),%esi
lea    0xde2(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1206 <main+0xb6>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
lea    0xe9c(%rip),%rdi
lea    0xc(%rsp),%rsi
xor    %eax,%eax
call   1070 <scanf@plt>
movslq 0xc(%rsp),%rbx
lea    0x0(,%rbx,4),%rdi
call   1060 <malloc@plt>
mov    %rax,%r15
test   %rbx,%rbx
jle    122c <main+0xdc>
lea    0xe6e(%rip),%r14
xor    %ebx,%ebx
nopw   0x0(%rax,%rax,1)
mov    $0x186a1,%edi
call   1060 <malloc@plt>
mov    %rax,%r13
mov    $0x186a1,%edi
call   1060 <malloc@plt>
mov    %rax,%r12
mov    %r14,%rdi
mov    %r13,%rsi
xor    %eax,%eax
call   1070 <scanf@plt>
mov    %r13,%rdi
mov    %r12,%rsi
xor    %eax,%eax
call   1040 <cost_for_substring@plt>
mov    %eax,(%r15,%rbx,4)
mov    %r13,%rdi
call   1030 <free@plt>
mov    %r12,%rdi
call   1030 <free@plt>
add    $0x1,%rbx
movslq 0xc(%rsp),%rax
cmp    %rax,%rbx
jl     11a0 <main+0x50>
test   %eax,%eax
jle    122c <main+0xdc>
lea    0xe05(%rip),%r14
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    (%r15,%rbx,4),%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
add    $0x1,%rbx
movslq 0xc(%rsp),%rax
cmp    %rax,%rbx
jl     1210 <main+0xc0>
mov    %r15,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

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

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