#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const long double PI = 3.1415926535897932384626;
const long double eps = 1e-9;
long long power(long long x, long long y) {
  long long ans = 1;
  x = x % mod;
  while (y > 0) {
    if (y & 1) ans = (ans * x) % mod;
    y = y >> 1;
    x = (x * x) % mod;
  }
  return ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long t;
  cin >> t;
  while (t--) {
    long long n, a = 0, b = 0, tb = 0, flag;
    vector<pair<long long, long long> > v;
    string s;
    cin >> s;
    n = s.size();
    if (s[0] == 'A') {
      flag = 0;
      a++;
    } else {
      flag = 1;
      b++;
    }
    for (long long i = (long long)(1); i < (long long)(n); ++i) {
      if (s[i] == 'A') {
        if (flag) {
          v.push_back(make_pair(b, 1));
          b = 0;
        }
        flag = 0;
        a++;
      } else {
        if (!flag) {
          v.push_back(make_pair(a, 0));
          a = 0;
        }
        flag = 1;
        b++;
      }
    }
    if (flag)
      v.push_back(make_pair(b, 1));
    else
      v.push_back(make_pair(a, 0));
    a = 0;
    b = 0;
    for (long long i = (long long)(0); i < (long long)(v.size()); ++i) {
      if (v[i].second == 0)
        a += v[i].first;
      else {
        b += v[i].first;
        if (b >= a) {
          tb += (b - a);
          a = 0;
        } else {
          a -= b;
        }
        b = 0;
      }
    }
    if (tb % 2)
      cout << 1 + a;
    else
      cout << a;
    cout << '\n';
  }
  return 0;
}
