#include <iostream>
using namespace std;
int main() {
//code
int t;
cin>>t;
while(t--)
{
string s;
cin>>s;
int n=s.size();
int a[n][n];
for(int i=0;i<n;i++)
a[i][i]=1;
for(int l=2;l<=n;l++)//here changing the length of substring upto length of string.
{
for(int i=0;i<n-l+1;i++)
{
int j=i+l-1;
if(l==2&&s[i]==s[j]) //first time match character,then assign only 2
{
a[i][j]=2;
}else if(s[i]==s[j])
{
a[i][j]=a[i+1][j-1]+2; //lower digonal +2
}else{
a[i][j]=max(a[i+1][j],a[i][j-1]);
}
}
cout<<n-a[0][n-1]<<endl;
}
return 0;
}