use std::io::BufRead;
use std::mem::swap;
fn solve_abc(a: usize, b: usize, c: usize) -> usize {
let mut a = a;
let mut b = b;
let mut c = c;
if a < b {
swap(&mut a, &mut b);
};
if b < c {
swap(&mut b, &mut c);
};
if a < b {
swap(&mut a, &mut b);
};
if b == 0 {
0
} else {
2 + solve_abc(a - 1, b - 1, c)
}
}
fn solve(x: &str) -> usize {
let a = x.chars().filter(|c| *c == 'A').count();
let b = x.chars().filter(|c| *c == 'B').count();
let c = x.chars().filter(|c| *c == 'C').count();
solve_abc(a, b, c)
}
fn main() {
let stdin = std::io::stdin();
let stdin = stdin.lock();
let mut lines = stdin.lines();
let t: usize = lines.next().unwrap().unwrap().parse().unwrap();
for _ in 0..t {
let x = lines.next().unwrap().unwrap();
println!("{}", solve(&x));
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_sample() {
assert_eq!(solve("AB"), 2);
assert_eq!(solve("CABAC"), 4);
assert_eq!(solve("AAAA"), 0);
assert_eq!(solve("AABBCC"), 6);
}
}