#include <bits/stdc++.h>
using namespace std;
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
  os << '[';
  for (const auto &x : v) os << " " << x;
  return os << ']';
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int testcase;
  cin >> testcase;
  while (testcase--) {
    string str;
    cin >> str;
    int length = str.length();
    int count_A = 0, count_B = 0, answer = 0;
    bool got_A = false, got_B = false;
    for (int i = 0; i < length; i++) {
      if (str[i] == 'A') {
        if (count_A > 0)
          count_A++;
        else if (count_A == 0)
          count_A++;
        else if (count_B > 0) {
          count_A++;
        } else if (count_B == 0)
          count_A++;
      } else {
        if (count_B > 0 && count_A > 0) {
          if (str[i - 1] == 'B')
            count_B--;
          else
            count_A--;
          answer++;
        } else if (count_B > 0) {
          count_B--;
          answer++;
        } else if (count_A > 0) {
          count_A--;
          answer++;
        } else if (count_A == 0)
          count_B++;
        else if (count_B == 0)
          count_B++;
      }
    }
    cout << length - answer * 2 << endl;
  }
}
