#include <iostream>
#include <cstring>
#include <string>
using namespace std;
// CCAAABBBAA
//
const int N_MAX = 101;
// length of the segment, start of the segment
int dp1[N_MAX][N_MAX];
int dp2[N_MAX];
int solve(string s) {
memset(dp1, 0, sizeof(dp1));
memset(dp2, 0, sizeof(dp2));
for(int i = 0; i+1 < s.size(); ++i) {
if(s[i] != s[i+1]) {
dp1[2][i] = 1;
}
}
// i is the length of the segment
for(int i = 3; i <= s.size(); ++i) {
// j is the start of the segment
for(int j = 0; i+j <= s.size(); ++j) {
if(dp1[i-2][j+1] && s[j] != s[j+i-1]) {
dp1[i][j] = 1;
}
else {
for(int k = 2; k+2 <= i; ++k) {
if(dp1[k][j] && dp1[i-k][j+k]) {
dp1[i][j] = 1;
break;
}
}
}
}
}
/*
for(int i = 0; i < s.size(); ++i) {
for(int j = 0; j <= s.size(); ++j) {
cout<<i<<' '<<j<<' '<<dp1[j][i]<<'\n';
}
}
*/
// dp2[i] == the amount of characters that can
// be removed from 0...i-1
for(int i = 1; i <= s.size(); ++i) {
dp2[i] = dp2[i-1];;
for(int j = 0; j + 2 <= i; ++j) {
if(dp1[i-j][j]) {
dp2[i] = max(dp2[i], dp2[j] + i-j);
}
}
}
return dp2[s.size()];
}
int main() {
int tt;
cin>>tt;
for(int xx = 0; xx < tt; ++xx) {
string s;
cin>>s;
int ans = solve(s);
cout<<ans<<endl;
}
}