#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define M 1000000007
using namespace std;
int cnt[333];
int main() {
cin.tie(0);
cout.tie(0);
ios_base::sync_with_stdio();
int t;
cin >> t;
for(int g=0; g<t; g++) {
string s;
cin >> s;
for(int i='A'; i<='C'; i++) {
cnt[i] = 0;
}
list<char> ls;
for(auto u:s) {
cnt[u]++;
ls.push_back(u);
}
vector<pair<int,int>> v;
for(int i='A'; i<='C'; i++) {
v.push_back(make_pair(cnt[i], i));
}
bool ok = true;
int ans = 0;
while(ok && !ls.empty()) {
ok = false;
sort(v.rbegin(), v.rend());
auto it = ls.begin();
while(next(it, 1) != ls.end()) {
if((*it == v[0].second && *next(it, 1) == v[1].second) || (*it == v[1].second && *next(it, 1) == v[0].second)) {
ls.erase(it, next(it, 2));
ans += 2;
v[0].first--;
v[1].first--;
ok = true;
break;
}
it++;
}
if(ok)
continue;
it = ls.begin();
while(next(it, 1) != ls.end()) {
if((*it == v[0].second && *next(it, 1) == v[2].second) || (*it == v[2].second && *next(it, 1) == v[0].second)) {
ls.erase(it, next(it, 2));
ans += 2;
v[0].first--;
v[2].first--;
ok = true;
break;
}
it++;
}
}
cout << ans << "\n";
}
}