#include <bits/stdc++.h>
using namespace std;
void arrp(int a[], int s, int e, int in) {
  for (int i = s; i < e; i += in) cout << a[i] << " ";
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int ans = 0, start = -1, b = 0;
    string s;
    cin >> s;
    int n = s.length();
    int rightb = 0;
    for (int i = n - 1; i >= 0; i--) {
      if (s[i] == 'A') {
        if (!rightb)
          ans++;
        else
          rightb--;
      } else
        rightb++;
    }
    cout << (ans + (rightb & 1)) << endl;
  }
}
