#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 /*<<< orphan*/ scanf(char *, ...);
int strlen(char *);
int main(void) {
  int test;
  char input[100006] = {'\0'};
  int i, j, p, q;
  int c;
  int store_i, store_j;
  scanf("%d", &test);
  while (test--) {
    scanf("\n");
    scanf("%s", input);
    i = 0;
    j = strlen(input) - 1;
    c = 0;
    while (i < j) {
      if (input[i] != input[j]) {
        store_i = i;
        store_j = j;
        p = i + 1;
        q = j;
        c = 1;
        break;
      }
      ++i;
      --j;
    }
    if (c) {
      c = 0;
      while (p < q) {
        if (input[p] != input[q]) {
          c = 1;
          break;
        }
        ++p;
        --q;
      }
      if (c == 0)
        printf("%d\n", store_i);
      else
        printf("%d\n", store_j);
      continue;
    } else
      printf("-1\n");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x186d0,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x186b0(%rbp),%rdi
xor    %esi,%esi
mov    $0x186a6,%edx
call   1050 <memset@plt>
lea    0xe94(%rip),%rdi
lea    -0x8(%rbp),%rsi
mov    $0x0,%al
call   1060 <scanf@plt>
mov    -0x8(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x8(%rbp)
cmp    $0x0,%eax
je     1347 <main+0x207>
lea    0xe7a(%rip),%rdi
mov    $0x0,%al
call   1060 <scanf@plt>
lea    -0x186b0(%rbp),%rsi
lea    0xe5c(%rip),%rdi
mov    $0x0,%al
call   1060 <scanf@plt>
movl   $0x0,-0x186b4(%rbp)
lea    -0x186b0(%rbp),%rdi
call   1030 <strlen@plt>
sub    $0x1,%eax
mov    %eax,-0x186b8(%rbp)
movl   $0x0,-0x186c4(%rbp)
mov    -0x186b4(%rbp),%eax
cmp    -0x186b8(%rbp),%eax
jge    1274 <main+0x134>
movslq -0x186b4(%rbp),%rax
movsbl -0x186b0(%rbp,%rax,1),%eax
ff
movslq -0x186b8(%rbp),%rcx
movsbl -0x186b0(%rbp,%rcx,1),%ecx
ff
cmp    %ecx,%eax
je     1251 <main+0x111>
mov    -0x186b4(%rbp),%eax
mov    %eax,-0x186c8(%rbp)
mov    -0x186b8(%rbp),%eax
mov    %eax,-0x186cc(%rbp)
mov    -0x186b4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x186bc(%rbp)
mov    -0x186b8(%rbp),%eax
mov    %eax,-0x186c0(%rbp)
movl   $0x1,-0x186c4(%rbp)
jmp    1274 <main+0x134>
mov    -0x186b4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x186b4(%rbp)
mov    -0x186b8(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x186b8(%rbp)
jmp    11d7 <main+0x97>
cmpl   $0x0,-0x186c4(%rbp)
je     1334 <main+0x1f4>
movl   $0x0,-0x186c4(%rbp)
mov    -0x186bc(%rbp),%eax
cmp    -0x186c0(%rbp),%eax
jge    12f5 <main+0x1b5>
movslq -0x186bc(%rbp),%rax
movsbl -0x186b0(%rbp,%rax,1),%eax
ff
movslq -0x186c0(%rbp),%rcx
movsbl -0x186b0(%rbp,%rcx,1),%ecx
ff
cmp    %ecx,%eax
je     12d2 <main+0x192>
movl   $0x1,-0x186c4(%rbp)
jmp    12f5 <main+0x1b5>
mov    -0x186bc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x186bc(%rbp)
mov    -0x186c0(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x186c0(%rbp)
jmp    128b <main+0x14b>
cmpl   $0x0,-0x186c4(%rbp)
jne    131b <main+0x1db>
mov    -0x186c8(%rbp),%esi
lea    0xcf7(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    132f <main+0x1ef>
mov    -0x186cc(%rbp),%esi
lea    0xcde(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1177 <main+0x37>
lea    0xccf(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1177 <main+0x37>
xor    %eax,%eax
add    $0x186d0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x186b8,%rsp
lea    0x10(%rsp),%rdi
mov    $0x186a6,%edx
xor    %esi,%esi
call   1060 <memset@plt>
lea    0xe87(%rip),%rdi
lea    0xc(%rsp),%rsi
xor    %eax,%eax
call   1070 <scanf@plt>
subl   $0x1,0xc(%rsp)
jb     12b6 <main+0x166>
movabs $0xffffffff00000000,%rbp
ff ff ff
movabs $0x100000000,%r12
lea    0xe5d(%rip),%r14
lea    0xe51(%rip),%r15
lea    0x10(%rsp),%r13
jmp    11d7 <main+0x87>
nopl   0x0(%rax)
lea    0xe43(%rip),%rdi
call   1030 <puts@plt>
subl   $0x1,0xc(%rsp)
jb     12b6 <main+0x166>
mov    %r14,%rdi
xor    %eax,%eax
call   1070 <scanf@plt>
mov    %r15,%rdi
mov    %r13,%rsi
xor    %eax,%eax
call   1070 <scanf@plt>
mov    %r13,%rdi
call   1040 <strlen@plt>
cmp    $0x2,%eax
jl     11c0 <main+0x70>
mov    %eax,%edi
mov    %rdi,%rcx
shl    $0x20,%rcx
add    %rbp,%rcx
add    $0xfffffffffffffffe,%rdi
movabs $0x200000000,%rdx
xor    %esi,%esi
nopw   0x0(%rax,%rax,1)
movzbl 0x10(%rsp,%rsi,1),%ebx
cmp    0x11(%rsp,%rdi,1),%bl
jne    1250 <main+0x100>
add    $0x1,%rsi
add    %rbp,%rcx
lea    -0x1(%rdi),%rbx
add    %r12,%rdx
cmp    %rdi,%rsi
mov    %rbx,%rdi
jl     1220 <main+0xd0>
jmp    11c0 <main+0x70>
cs nopw 0x0(%rax,%rax,1)
add    $0xffffffff,%eax
sub    %rsi,%rax
lea    0x1(%rsi),%edi
cmp    %eax,%edi
jge    1292 <main+0x142>
lea    0x1(%rsi),%rdi
sar    $0x20,%rcx
cs nopw 0x0(%rax,%rax,1)
nop
movzbl 0x10(%rsp,%rdi,1),%ebx
cmp    0x10(%rsp,%rcx,1),%bl
jne    129b <main+0x14b>
add    $0xffffffffffffffff,%rcx
add    $0x1,%rdi
mov    %rdx,%rbx
sar    $0x20,%rbx
add    %r12,%rdx
cmp    %rcx,%rbx
jl     1270 <main+0x120>
lea    0xd6d(%rip),%rdi
jmp    12a4 <main+0x154>
lea    0xd64(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
subl   $0x1,0xc(%rsp)
jae    11d7 <main+0x87>
xor    %eax,%eax
add    $0x186b8,%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 = [2 x i8] c"\0A\00"
@.str.2 = [3 x i8] c"%s\00"
@.str.3 = [4 x i8] c"%d\0A\00"
@.str.4 = [4 x i8] c"-1\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [100006 x i8]
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 0, i32* %1
%11 = bitcast [100006 x i8]* %3 to i8*
%12 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %2)
br label %13
13: ; preds = %89, %86, %0
%14 = load i32, i32* %2
%15 = add nsw i32 %14, -1
store i32 %15, i32* %2
%16 = icmp ne i32 %14, 0
br i1 %16, label %17, label %90
17: ; preds = %13
%18 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%19 = getelementptr inbounds [100006 x i8], [100006 x i8]* %3, i64 0, i64 0
%20 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %19)
store i32 0, i32* %4
%21 = getelementptr inbounds [100006 x i8], [100006 x i8]* %3, i64 0, i64 0
%22 = call i32 @strlen(i8* %21)
%23 = sub nsw i32 %22, 1
store i32 %23, i32* %5
store i32 0, i32* %8
br label %24
24: ; preds = %46, %17
%25 = load i32, i32* %4
%26 = load i32, i32* %5
%27 = icmp slt i32 %25, %26
br i1 %27, label %28, label %51
28: ; preds = %24
%29 = load i32, i32* %4
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds [100006 x i8], [100006 x i8]* %3, i64 0, i64 %30
%32 = load i8, i8* %31
%33 = sext i8 %32 to i32
%34 = load i32, i32* %5
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds [100006 x i8], [100006 x i8]* %3, i64 0, i64 %35
%37 = load i8, i8* %36
%38 = sext i8 %37 to i32
%39 = icmp ne i32 %33, %38
br i1 %39, label %40, label %46
40: ; preds = %28
%41 = load i32, i32* %4
store i32 %41, i32* %9
%42 = load i32, i32* %5
store i32 %42, i32* %10
%43 = load i32, i32* %4
%44 = add nsw i32 %43, 1
store i32 %44, i32* %6
%45 = load i32, i32* %5
store i32 %45, i32* %7
store i32 1, i32* %8
br label %51
46: ; preds = %28
%47 = load i32, i32* %4
%48 = add nsw i32 %47, 1
store i32 %48, i32* %4
%49 = load i32, i32* %5
%50 = add nsw i32 %49, -1
store i32 %50, i32* %5
br label %24
51: ; preds = %40, %24
%52 = load i32, i32* %8
%53 = icmp ne i32 %52, 0
br i1 %53, label %54, label %87
54: ; preds = %51
store i32 0, i32* %8
br label %55
55: ; preds = %72, %54
%56 = load i32, i32* %6
%57 = load i32, i32* %7
%58 = icmp slt i32 %56, %57
br i1 %58, label %59, label %77
59: ; preds = %55
%60 = load i32, i32* %6
%61 = sext i32 %60 to i64
%62 = getelementptr inbounds [100006 x i8], [100006 x i8]* %3, i64 0, i64 %61
%63 = load i8, i8* %62
%64 = sext i8 %63 to i32
%65 = load i32, i32* %7
%66 = sext i32 %65 to i64
%67 = getelementptr inbounds [100006 x i8], [100006 x i8]* %3, i64 0, i64 %66
%68 = load i8, i8* %67
%69 = sext i8 %68 to i32
%70 = icmp ne i32 %64, %69
br i1 %70, label %71, label %72
71: ; preds = %59
store i32 1, i32* %8
br label %77
72: ; preds = %59
%73 = load i32, i32* %6
%74 = add nsw i32 %73, 1
store i32 %74, i32* %6
%75 = load i32, i32* %7
%76 = add nsw i32 %75, -1
store i32 %76, i32* %7
br label %55
77: ; preds = %71, %55
%78 = load i32, i32* %8
%79 = icmp eq i32 %78, 0
br i1 %79, label %80, label %83
80: ; preds = %77
%81 = load i32, i32* %9
%82 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %81)
br label %86
83: ; preds = %77
%84 = load i32, i32* %10
%85 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %84)
br label %86
86: ; preds = %83, %80
br label %13
87: ; preds = %51
%88 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0))
br label %89
89: ; preds = %87
br label %13
90: ; preds = %13
ret i32 0
}

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