#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 */
char getchar();
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ scanf(char *, int *);
int main() {
  int t, n;
  scanf("%d", &t);
  while (t--) {
    int k = 0, hope[26] = {0};
    scanf("%d ", &n);
    for (int i = 0; i < n; ++i) {
      int c = getchar() - 'a';
      if (hope[c] - i > k)
        k = hope[c] - i;
      hope[c] = n + i;
    }
    printf("%d\n", k);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xea7(%rip),%rdi
lea    -0x8(%rbp),%rsi
call   1060 <scanf@plt>
mov    -0x8(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x8(%rbp)
cmp    $0x0,%eax
je     1233 <main+0xf3>
movl   $0x0,-0x10(%rbp)
lea    -0x80(%rbp),%rdi
xor    %esi,%esi
mov    $0x68,%edx
call   1040 <memset@plt>
lea    0xe6f(%rip),%rdi
lea    -0xc(%rbp),%rsi
call   1060 <scanf@plt>
movl   $0x0,-0x84(%rbp)
mov    -0x84(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    121f <main+0xdf>
mov    $0x0,%al
call   1050 <getchar@plt>
movsbl %al,%eax
sub    $0x61,%eax
mov    %eax,-0x88(%rbp)
movslq -0x88(%rbp),%rax
mov    -0x80(%rbp,%rax,4),%eax
sub    -0x84(%rbp),%eax
cmp    -0x10(%rbp),%eax
jle    11f7 <main+0xb7>
movslq -0x88(%rbp),%rax
mov    -0x80(%rbp,%rax,4),%eax
sub    -0x84(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%ecx
add    -0x84(%rbp),%ecx
movslq -0x88(%rbp),%rax
mov    %ecx,-0x80(%rbp,%rax,4)
mov    -0x84(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x84(%rbp)
jmp    11a7 <main+0x67>
mov    -0x10(%rbp),%esi
lea    0xdde(%rip),%rdi
call   1030 <printf@plt>
jmp    1162 <main+0x22>
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    $0x78,%rsp
lea    0xebb(%rip),%rdi
lea    0xc(%rsp),%rsi
call   1050 <scanf@plt>
subl   $0x1,0xc(%rsp)
jae    1167 <main+0x37>
xor    %eax,%eax
add    $0x78,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe95(%rip),%r14
lea    0x8(%rsp),%r15
lea    0xe8d(%rip),%r12
jmp    118f <main+0x5f>
nopl   0x0(%rax)
mov    %r12,%rdi
call   1030 <printf@plt>
subl   $0x1,0xc(%rsp)
jb     1156 <main+0x26>
xorps  %xmm0,%xmm0
movaps %xmm0,0x60(%rsp)
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
movq   $0x0,0x70(%rsp)
mov    %r14,%rdi
mov    %r15,%rsi
call   1050 <scanf@plt>
cmpl   $0x0,0x8(%rsp)
mov    $0x0,%esi
jle    1180 <main+0x50>
xor    %ebp,%ebp
xor    %r13d,%r13d
xor    %ebx,%ebx
nopw   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1040 <getchar@plt>
movsbq %al,%rax
mov    -0x174(%rsp,%rax,4),%esi
add    %ebp,%esi
cmp    %r13d,%esi
cmovle %r13d,%esi
mov    0x8(%rsp),%ecx
lea    (%rbx,%rcx,1),%edx
mov    %edx,-0x174(%rsp,%rax,4)
add    $0x1,%ebx
add    $0xffffffff,%ebp
mov    %esi,%r13d
cmp    %ecx,%ebx
jl     11e0 <main+0xb0>
jmp    1180 <main+0x50>

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

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