#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ;
  long long t;
  cin >> t;
  while (t--) {
    string s;
    cin >> s;
    long long n = s.length();
    long long i = 0, j = 0;
    while (true) {
      if (i >= n) {
        break;
      }
      if (s[i] == 'A' && s[i + 1] == 'B') {
        s.erase(i, 2);
        i = j - 1;
        j = i;
        continue;
      } else if (s[i] == 'B' && s[i + 1] == 'B') {
        s.erase(i, 2);
        i = j - 1;
        j = i;
        continue;
      }
      i++;
      j = i;
    }
    cout << s.size() << "\n";
  }
  return 0;
}
