#include <bits/stdc++.h>
using namespace std;
void solve() {
  long long n, m, i, j, k, m1 = 0;
  string s;
  cin >> s;
  n = s.size();
  i = 0;
  while (i < n - 1 and s != "") {
    if (s[i] != s[i + 1] and s[i] == 'A')
      s.erase(i, 2), n -= 2, i -= 2;
    else if (s[i] == s[i + 1] and s[i] == 'B')
      s.erase(i, 2), n -= 2, i -= 2;
    else {
      i++;
    }
  }
  m = s.size();
  m -= m1;
  cout << m << "\n";
}
int main() {
  long long t;
  cin >> t;
  while (t-- > 0) {
    solve();
  }
}
