import sys
input = sys.stdin.readline
def pseudo(a, left, right):
while left < right:
if a[left] == a[right]:
left += 1
right -= 1
else:
return False
return True
def palindrome(a, left, right):
while left < right:
if a[left] == a[right]:
left += 1
right -= 1
else:
res1 = pseudo(a, left+1, right)
res2 = pseudo(a, left, right-1)
if res1 == True or res2 == True:
return 1
else:
return 2
return 0
n = int(input())
for _ in range(n):
string = input().rstrip()
res = palindrome(string, 0, len(string)-1)
print(res)