#include <bits/stdc++.h>
using namespace std;
int32_t main() {
  long long x;
  cin >> x;
  while (x--) {
    string s;
    cin >> s;
    stack<char> a_stack;
    stack<char> b_stack;
    for (long long i = 0; i < s.length(); i++) {
      if (s[i] == 'A') {
        a_stack.push('A');
      }
      if (s[i] == 'B') {
        if (!a_stack.empty()) {
          a_stack.pop();
          continue;
        } else if (!b_stack.empty()) {
          b_stack.pop();
          continue;
        } else {
          b_stack.push('B');
        }
      }
    }
    cout << a_stack.size() + b_stack.size() << endl;
  }
  return 0;
}
