#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
const int N = 111;
string rot(string s) {
int A, B, C;
A = 0; B = 0; C = 0;
int n = s.size();
for(int i = 0; i < n; ++i) {
if(s[i] == 'A') ++A;
if(s[i] == 'B') ++B;
if(s[i] == 'C') ++C;
}
if(A < B) {
string t;
for(int i = 0; i < n; ++i) {
if(s[i] == 'A') t += 'B';
if(s[i] == 'B') t += 'A';
if(s[i] == 'C') t += 'C';
}
s = t;
swap(A, B);
}
if(A < C) {
string t;
for(int i = 0; i < n; ++i) {
if(s[i] == 'A') t += 'C';
if(s[i] == 'B') t += 'B';
if(s[i] == 'C') t += 'A';
}
s = t;
swap(A, C);
}
if(B < C) {
string t;
for(int i = 0; i < n; ++i) {
if(s[i] == 'A') t += 'A';
if(s[i] == 'B') t += 'C';
if(s[i] == 'C') t += 'B';
}
s = t;
swap(B, C);
}
return s;
}
int main() {
iostream::sync_with_stdio(false);
cin.tie(0);
int q;
cin >> q;
while(q > 0) {
--q;
string s;
cin >> s;
int ans = 0;
while(true) {
s = rot(s);
int n = s.size();
int A, B, C;
A = 0; B = 0; C = 0;
for(int i = 0; i < n; ++i) {
if(s[i] == 'A') ++A;
if(s[i] == 'B') ++B;
if(s[i] == 'C') ++C;
}
if(B == 0) break;
bool w = false;
for(int i = 0; i < n-1; ++i) {
if((s[i] == 'A' && s[i+1] == 'B') || (s[i] == 'B' && s[i+1] == 'A')) {
string t;
for(int j = 0; j < n; ++j) {
if(j == i || j == i+1) continue;
t += s[j];
}
s = t;
++ans;
w = true;
break;
}
}
if(w) continue;
for(int i = 0; i < n-1; ++i) {
if((s[i] == 'A' && s[i+1] == 'C') || (s[i] == 'C' && s[i+1] == 'A')) {
string t;
for(int j = 0; j < n; ++j) {
if(j == i || j == i+1) continue;
t += s[j];
}
s = t;
++ans;
w = true;
break;
}
}
}
cout << 2*ans << "\n";
}
}