use std::io::{stdin, BufRead, BufReader};
fn main() {
let mut stdin = BufReader::new(stdin());
let mut cur_line = String::new();
stdin.read_line(&mut cur_line).unwrap();
let t: usize = cur_line
.trim()
.parse().unwrap();
for _ in 0..t {
cur_line.clear();
stdin.read_line(&mut cur_line).unwrap();
let v = cur_line.as_bytes();
let mut na = 0;
let mut nb = 0;
for b in v {
match b {
b'A' => na += 1,
b'B' => nb += 1,
_ => (),
}
}
let mut x = 0;
for i in 0..na {
if v[i] == b'B' {
x += 1;
}
}
println!("{}", x);
}
}