#include <bits/stdc++.h>
using namespace std;
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long t;
  cin >> t;
  while (t--) {
    string s;
    cin >> s;
    long long n = s.size();
    vector<long long> a{};
    long long cnt = 0;
    char c = '*';
    for (long long i = 0; i < n; i++) {
      if (i == 0) {
        c = s[i];
        cnt++;
        continue;
      }
      if (c != s[i]) {
        a.push_back(cnt);
        c = s[i];
        cnt = 1;
      } else
        cnt++;
    }
    a.push_back(cnt);
    long long j = a.size();
    if (j == 1) {
      if (s[n - 1] == 'A')
        cout << n << endl;
      else
        cout << n % 2 << endl;
      continue;
    } else if (j == 2) {
      if (s[n - 1] == 'A') {
        cout << a[0] % 2 + a[1] << endl;
      } else {
        if (a[0] >= a[1]) {
          cout << a[0] - a[1] << endl;
        } else
          cout << (a[1] - a[0]) % 2 << endl;
      }
      continue;
    }
    if (s[n - 1] == 'B')
      j = j - 2;
    else if (s[n - 1] == 'A')
      j = j - 3;
    long long carry = 0;
    while (j >= 0) {
      a[j + 1] += carry;
      carry = 0;
      if (a[j] >= a[j + 1]) {
        a[j] = a[j] - a[j + 1];
        a[j + 1] = 0;
        j = j - 2;
      } else {
        carry += a[j + 1] - a[j];
        a[j + 1] = 0;
        a[j] = 0;
        j = j - 2;
      }
    }
    if (j == -1) {
      a[j + 1] += carry;
      carry = 0;
    }
    long long ans = 0;
    ans += carry % 2;
    for (long long i = 0; i < a.size(); i++) {
      if (j == -1 && i == 0) {
        ans += a[i] % 2;
        continue;
      }
      ans += a[i];
    }
    cout << ans << endl;
  }
  return 0;
}
