public static void main(String[] args) {

		// System.out.println(new String(erase(-1, "abbcca".getBytes())));
		Scanner in = new Scanner(System.in);
		int total = in.nextInt();
		for (int counter = 0; counter < total; counter++) {
			String input = in.next();
			byte[] src = input.getBytes();
			int result = 0;
			for (int i = 0; i <= src.length; i++) {
				for (int j = 65; j < 68; j++) {
					byte[] dest = new byte[src.length + 1];
					System.arraycopy(src, 0, dest, 0, i);
					if (i < src.length) {
						System.arraycopy(src, i, dest, i + 1, src.length - i);
					}
					dest[i] = (byte) j;
					// System.out.println(new String(dest));
					byte[] done = erase(-1, dest);
					if (result < (dest.length - done.length)) {
						result = dest.length - done.length;
					}
				}
			}
			System.out.println(result);
		}
	}