package homework;
import java.util.Scanner;
public class Homework1039 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
sc.nextLine();
String[] arr = new String[n];
for(int i = 0; i < n;i++) {
arr[i] = sc.nextLine();
}
for (String string : arr) {
System.out.println(getMax(string));
}
sc.close();
}
public static int getMax(String s1) {
int max = 0;
//1.插入
for(char insert = 'A'; insert < 'D'; insert++) {
for(int i = 1; i <= s1.length(); i++) {
String s2 = s1.substring(0, i) + insert + s1.substring(i);
//2.切割
while(true) {
StringBuffer sb = new StringBuffer();
int counter = 1;
for(int j = 1; j <= s2.length(); j++) {
if(j != s2.length() && s2.charAt(j) == s2.charAt(j - 1)) {
counter++;
}else {
if(counter == 1) {
sb.append(s2.charAt(j - 1));
}
counter = 1;
}
}
if(s2.length() == sb.length()) {
break;
}
s2 = sb.toString();
}
if(s1.length() - s2.length() > max) {
max = (s1.length() + 1) - s2.length();
}
}
}
return max;
}
}