#include <bits/stdc++.h>
using namespace std;
#define i64 long long
const int N = 1e6 + 1e5;
int Next[N], c[27], tail[N];
i64 sum[N][27];
int h[N], x[N], last[N], ne[N];
int n, idx;
#define gc() (p1 == p2 && (p2 = (p1 = ibuf) + fread(ibuf, 1, SIZE, stdin), p1 == p2) ? EOF : *p1++)
namespace FastIO { static constexpr int SIZE = 1 << 21; char ibuf[SIZE], obuf[SIZE], *p1 = ibuf, *p2 = ibuf, *p3 = obuf; inline void read(char& c) { for(c = gc(); !std::isgraph(c); c = gc()); } inline void read(char* s) { char c = gc(); for(; !std::isgraph(c); c = gc());for(; std::isgraph(c); c = gc()) *s++ = c; *s = 0; } inline void read(std::string& s) { s.clear(); char c = gc(); for(; !std::isgraph(c); c = gc()); for(; std::isgraph(c); c = gc()) s.push_back(c); } inline void pc(char c) { if(p3 - obuf == SIZE) fwrite(obuf, 1, SIZE, stdout), p3 = obuf; *p3++ = c; } inline void write(char c) { pc(c); } inline void write(const char* s) { while(*s) pc(*s), ++s; } inline void write(std::string s) { for(const char c : s) pc(c); } template<typename _Tp>inline void read(_Tp& x) { x = 0; char c = gc(); int f = 0; for(; !std::isdigit(c); c = gc()) f |= c == '-'; for(; std::isdigit(c); c = gc()) x = (x << 1) + (x << 3) + (c ^ 48); return f ? x = ~x + 1 : 1, void(); } template<typename _Tp>inline void write(_Tp x) { static int stk[40]; int tp = 0; if(!x) return pc('0'), void(); if(x < 0) pc('-'), x = ~x + 1; while(x) stk[++tp] = x % 10, x /= 10; while(tp) pc(stk[tp--] + '0'); } template<typename _Tp>inline void writesp(_Tp x) { write(x), pc(' '); } template<typename _Tp>inline void writeln(_Tp x) { write(x), pc('\n'); } template<typename _Tp, typename ...Args>inline void read(_Tp& x, Args& ...args) { read(x), read(args...); } template<typename _Tp, typename ...Args>inline void write(_Tp x, Args ...args) { write(x), write(args...); } template<typename _Tp, typename ...Args>inline void writesp(_Tp x, Args ...args) { writesp(x), writesp(args...); } template<typename _Tp, typename ...Args>inline void writeln(_Tp x, Args ...args) { writeln(x), writeln(args...); } inline void flush() { fwrite(obuf, p3 - obuf, 1, stdout); } }
using namespace FastIO;
inline void add(int u, int v, int w){
ne[idx] = h[u], x[idx] = v, last[idx] = w;
h[u] = idx++;
return;
}
inline void solve(){
string s;
read(s);
n = s.size(), idx = 0;
s = '$' + s;
for(int i = 1; i <= n; i++)
h[i] = -1;
Next[1] = 0;
for(int i = 2, j = 0; i <= n; i++){
while( j && s[i] != s[j + 1] )
j = Next[j];
if( s[i] == s[j + 1] )
Next[i] = ++j;
else
Next[i] = 0;
}
for(int i = 1; i < n; i++){
if( i % (i - Next[i]) == 0 )
add(i / (i - Next[i]), i - Next[i], n - i);
else
add(1, i, n - i);
}
for(int i = 1; i <= 26; i++)
c[i] = 0;
int now = 0;
for(int i = 1; i <= n; i++){
for(int j = 0; j <= 26; j++)
sum[i][j] = sum[i - 1][j];
if( c[s[i] - 'a' + 1] & 1 )
now--;
else
now++;
c[s[i] - 'a' + 1]++;
sum[i][now]++;
}
for(int i = 1; i <= n; i++)
for(int j = 1; j <= 26; j++)
sum[i][j] += sum[i][j - 1];
for(int i = 1; i <= 26; i++)
c[i] = 0;
for(int i = n; i >= 1; i--){
if( c[s[i] - 'a' + 1] & 1 )
tail[n - i + 1] = tail[n - i] - 1;
else
tail[n - i + 1] = tail[n - i] + 1;
c[s[i] - 'a' + 1]++;
}
i64 ans = 0;
for(int i = 1; i <= n; i++)
for(int j = 1; i * j <= n; j++)
if( h[i * j] != -1 )
for(int k = h[i * j]; ~k; k = ne[k])
ans += sum[i * x[k] - 1][tail[last[k]]];
writeln(ans);
return;
}
int main(){
int T;
cin >> T;
while( T-- )
solve();
flush();
return 0;
}