#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 1;
long long i, j;
void solve() {
  string s;
  cin >> s;
  long long n = s.size();
  long long stack_length = 0;
  for (auto c : s) {
    if (c == 'A')
      stack_length++;
    else {
      if (stack_length == 0)
        stack_length++;
      else
        stack_length--;
    }
  }
  cout << stack_length << "\n";
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long t = 1;
  cin >> t;
  while (t--) solve();
  return 0;
}
