#include <bits/stdc++.h>
using namespace std;
template <class T>
bool ckmin(T& a, const T& b) {
  return b < a ? a = b, 1 : 0;
}
template <class T>
bool ckmax(T& a, const T& b) {
  return a < b ? a = b, 1 : 0;
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int main() {
  cin.tie(0)->sync_with_stdio(0);
  cin.exceptions(cin.failbit);
  int te;
  cin >> te;
  while (te--) {
    string s;
    cin >> s;
    int n = (int)(s).size();
    int a = 0, b = 0;
    for (int i = (0); i < (n); ++i) {
      if (s[i] == 'A')
        ++a;
      else {
        if (a)
          --a;
        else if (b)
          --b;
        else
          ++b;
      }
    }
    cout << a + b << '\n';
  }
}
