#include <bits/stdc++.h>
typedef long long ll;
#define M 1000000007
using namespace std;
map<string, int> m;
int rec(string s) {
int n = (int)s.length();
int best = n;
if (m[s]) return m[s];
for (int i = 1; i < n; ++i) {
if (s[i-1] != s[i]) {
string ns;
for (int j = 0; j < n; ++j) {
if (j == i - 1) {
j++;
continue;
}
ns += s[j];
}
best = min(best, rec(ns));
}
}
m[s] = best;
return best;
}
void test_case() {
string s;
cin >> s;
int n = (int)s.length();
//cout << "here" << endl;
cout << n - rec(s);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
for (int i = 0; i < t; ++i) {
test_case();
cout << "\n";
}
return 0;
}