let fs = require('fs');
let input = fs.readFileSync('/dev/stdin').toString().split('\n');
// input = [7, 'abba', 'summuus', 'xabba', 'xabbay', 'comcom', 'comwwmoc', 'comwwtmoc'];
let number = Number(input[0]);
for(let i = 1 ; i <= number ; i++) {
let string = input[i].split("").map(String);
let width = string.length;
let middle = parseInt(width / 2);
let temp = string[middle];
let typeA = string.slice(0, middle);
let typeB = [];
if(width % 2 === 0) {
typeB = string.slice(middle).reverse();
if(typeA.join("") === typeB.join("")) {
console.log(0);
} else {
if(temp === typeB[0]) {
console.log(1);
}
console.log(2);
}
} else {
typeB = string.slice(middle + 1).reverse();
if(typeA.join("") === typeB.join("")) {
console.log(0);
} else {
typeB.pop();
typeB.push(temp);
if(typeA.join("") === typeB.join("")) {
console.log(1);
} else {
console.log(2);
}
}
}
}
// 예제 중 'xabba'만 통과가 안됐다. 나중에 내 풀이로 다시 한번 도전해보자.