#include <bits/stdc++.h>
typedef long long ll;
#define M 1000000007
using namespace std;
int rec(string s) {
int n = (int)s.length();
int best = n;
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));
}
}
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;
}