import java.util.*;
public class abc {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
//int t = input.nextInt();
int t = Integer.parseInt(input.nextLine());
int aqty = 0;
int bqty = 0;
int cqty = 0;
int maxqty = 0;
int minqty = 0;
int rest = 0;
int ans = 0;
for (int i = 0; i < t; i++) {
String abc = input.nextLine();
int lngt = abc.length();
String a = abc.replaceAll("B","");
a = a.replaceAll("C","");
aqty = a.length();
String b = abc.replaceAll("A","");
b = b.replaceAll("C","");
bqty = b.length();
maxqty = Math.max(aqty, bqty);
minqty = Math.min(aqty, bqty);
cqty = lngt - aqty - bqty;
maxqty = Math.max(maxqty, cqty);
minqty = Math.min(minqty, cqty);
rest = lngt - maxqty - minqty;
if ((rest + minqty) >= maxqty) ans = lngt - (lngt % 2);
else ans = (rest + minqty) * 2;
System.out.println(ans);
}
input.close();
}
}