#include <bits/stdc++.h>
using namespace std;
long long int in, i, j, k, n;
const long long int N = 5e5 + 7, mod = 1e9 + 7;
vector<long long int> adj[2000005];
bool vis[2000005];
long long int f[N];
bool compare(pair<long long int, long long int> a,
             pair<long long int, long long int> b) {
  if (a.first > b.first) {
    return a > b;
  } else if (a.first == b.first) {
    return a.second > b.second;
  } else {
    return b > a;
  }
}
bool dfs(long long int s, vector<bool> &recStack) {
  if (!vis[s]) {
    vis[s] = recStack[s] = true;
    for (auto x : adj[s]) {
      if (!vis[x] && dfs(x, recStack)) {
        return true;
      } else if (recStack[x]) {
        return true;
      }
    }
  }
  recStack[s] = false;
  return false;
}
long long int POW(long long int a, long long int b) {
  a %= mod;
  long long int r = 1;
  for (long long int i = b; i > 0; i = i >> 1) {
    if (i & 1) r = (r * a) % mod;
    a = (a * a) % mod;
  }
  return r;
}
long long int nCr(long long int n, long long int r) {
  if (n < r) return 0;
  return f[n] * POW(f[n - r] * f[r], mod - 2) % mod;
}
long long int up(long long int a, long long int b) {
  return a / b + (a % b != 0);
}
bool is_prime(long long int n) {
  if (n == 2 || n == 3) {
    return true;
  }
  if (n == 1 || n % 2 == 0 || n % 3 == 0) {
    return false;
  }
  for (int i = 5; i * i <= n; i += 6) {
    if (n % i == 0 || n % (i + 2) == 0) {
      return false;
    }
  }
  return true;
}
class MinHeap {
 public:
  int *a;
  int size, capacity;
  MinHeap(int n) {
    a = new int[n];
    size = 0;
    capacity = n;
  }
  int left(int i) { return 2 * i + 1; }
  int right(int i) { return 2 * i + 2; }
  int parent(int i) { return (i - 1) / 2; }
  void minHeapify(int i);
  void insert(int x);
};
void MinHeap::insert(int x) {
  if (size == capacity) return;
  size++;
  a[size - 1] = x;
  for (int i = size - 1; i != 0 && a[parent(i)] > a[i];) {
    swap(a[parent(i)], a[i]);
    i = parent(i);
  }
}
void MinHeap::minHeapify(int i) {
  int l = left(i), r = right(i), mini = i;
  if (l < size && a[l] < a[mini]) {
    mini = l;
  }
  if (r < size && a[r] < a[mini]) {
    mini = r;
  }
  if (mini != i) {
    swap(a[mini], a[i]);
    minHeapify(mini);
  }
}
void solve() {
  string s;
  cin >> s;
  n = s.size();
  vector<bool> f(n);
  fill(f.begin(), f.end(), false);
  long long int false_so_far = 1;
  long long int count = 0;
  for (i = 1; i < n; i++) {
    if (s[i] == 'B') {
      if (false_so_far > 0) {
        count += 2;
        false_so_far--;
      } else {
        false_so_far++;
      }
    } else {
      false_so_far++;
    }
  }
  cout << n - count << endl;
}
int main() {
  f[0] = 1;
  for (long long int i = 1; i < N; i++) {
    f[i] = (f[i - 1] * i) % mod;
  }
  long long int t = 1;
  cin >> t;
  while (t--) {
    solve();
  }
}
