#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
const int M = 5e5 + 5;
const int OO = 1e9;
const int mod = 1e9 + 7;
int t, cntA, cntB, mat, A;
string s;
void init() { cntA = cntB = mat = A = 0; }
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> t;
  while (t--) {
    init();
    cin >> s;
    for (auto& i : s) {
      if (i == 'A')
        ++cntA, ++A;
      else {
        ++cntB;
        if (A) {
          ++mat;
          --A;
        }
      }
    }
    cout << (cntA - mat) + ((cntB - mat) & 1) << '\n';
  }
  return 0;
}
