#include <bits/stdc++.h>
using namespace std;
bool is_palindrome(string s)
{
int n = s.size();
for (int i = 0; i < n / 2; i++)
{
if (s[i] != s[n - i - 1])
{
return false;
}
}
return true;
}
int distance(string s, string t)
{
if (s.size() < t.size())
{
swap(s, t);
}
int dist = 0;
while (s.size() > t.size())
{
s.erase(s.begin());
s.pop_back();
dist++;
}
while (s.size() && s != t)
{
s.erase(s.begin());
s.pop_back();
t.erase(t.begin());
t.pop_back();
dist += 2;
}
return dist;
}
int main()
{
int t = 0;
cin >> t;
while (t--)
{
string s;
cin >> s;
int n = s.size();
vector<string> palindromes;
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j+=2)
{
string sub = s.substr(i, j - i + 1);
if (is_palindrome(sub))
{
palindromes.push_back(sub);
}
}
}
// for (auto x: palindromes) cout << x << " "; cout << endl;
int ans = 0;
for (int i = 0; i < palindromes.size(); i++)
{
for (int j = i + 1; j < palindromes.size(); j++)
{
ans += distance(palindromes[i], palindromes[j]);
// cout << palindromes[i] << " " << palindromes[j] << " " << distance(palindromes[i], palindromes[j]) << endl;
}
}
cout << ans << endl;
}
return 0;
}