#include <bits/stdc++.h>
using namespace std;
void solve() {
  string num;
  cin >> num;
  int len = num.length() - 1;
  int cur = 0;
  int m = 0;
  for (int i = len; i >= 0; i--) {
    if (num[i] == 'A')
      cur--;
    else
      cur++;
    m = min(m, cur);
  }
  int ca = 0;
  for (auto i : num)
    if (i == 'A') ca++;
  cur = ca + m;
  int cb = num.length() - ca;
  cb -= cur;
  cur += cb / 2;
  cout << num.length() - 2 * cur << endl;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int test;
  cin >> test;
  for (int i = 1; i <= test; i++) {
    solve();
  }
  return 0;
}
