1//Code by @soumyadeepp : Soumyadeep Ghosh
2
3//A very Simple DP problem. Just find the length of the Longest common subsequence
4//of the string and its reverse. Now subtract this length from the actual length
5//of the substring. That's it.
6
7#include <bits/stdc++.h>
8
9using namespace std;
10
11int lps(string s)
12{
13 string r = s;
14 reverse(r.begin(),r.end());
15 int sz1 = s.size();
16 int dp[sz1+1][sz1+1];
17
18 for(int i = 0 ; i <= sz1 ; i++)
19 {
20 dp[0][i] = 0;
21 dp[i][0] = 0;
22 }
23 for(int i = 1; i <= sz1; i++)
24 {
25 for(int j = 1; j <= sz1 ; j++)
26 {
27 if(s[i-1] == r[j-1])
28 {
29 dp[i][j] = dp[i-1][j-1] + 1;
30 }
31 else
32 dp[i][j] = max(dp[i-1][j],dp[i][j-1]);
33 }
34 }
35 return dp[sz1][sz1];
36}
37int main()
38{
39 int t;
40 cin>>t;
41 while(t--)
42 {
43 string s;
44 cin>>s;
45 int ans = s.size() - lps(s);
46 cout<<ans<<endl;
47 }
48}