#include <algorithm>
#include <iostream>
#include <map>
#include <string>
using namespace std;
map<string, int> cache;
int slow(const string &s) {
if (s.size() == 0) return 0;
if (cache.find(s) != cache.end()) return cache[s];
int maxways = 0;
for (int i = 0; i < s.size() - 1; ++i) {
if (s[i] != s[i+1]) {
string s2 = s;
s2.erase(i, 2);
//auto asd = slow(s2) + 2;
//if (asd < maxways) {
// cout << s << " -> " << s2 << " was bad" << endl;
//}
maxways = max(maxways, slow(s2) + 2);
}
}
cache[s] = maxways;
return maxways;
}
int brute(const string &s) {
if (s.size() == 0) return 0;
int counts[3] = {0, 0, 0};
for (int i = 0; i < s.size(); ++i) {
++counts[s[i]-'A'];
}
int maxind = distance(counts, max_element(counts, counts + 3));
for (int i = 0; i < s.size() - 1; ++i) {
if (s[i] != s[i+1] && (s[i] - 'A' == maxind || s[i+1] - 'A' == maxind)) {
string s2 = s;
s2.erase(i, 2);
return brute(s2) + 2;
}
}
return 0;
}
int main()
{
//string in = "AAAAAAAABBB";
//do {
// if (slow(in) != brute(in)) {
// cout << in << slow(in) << brute(in) << endl;
// }
//} while(next_permutation(in.begin(), in.end()));
int t;
cin >> t;
for (int i = 0; i < t; ++i) {
string s;
cin >> s;
cout << brute(s) << endl;
}
}