//优秀的拆分
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define ll long long
#define M(a) memset(a,0,sizeof a)
#define fo(i,j,k) for(i=j;i<=k;i++)
using namespace std;
const int mxn=30005;
int m,T,len;
char s[mxn];
int f[mxn],g[mxn]; //fi:以i结尾,gi:以i开头
struct node
{
int st[mxn][30],rank[mxn],height[mxn];
int a[mxn],b[mxn],x[mxn],y[mxn],sa[mxn];
inline bool comp(int i,int j,int l)
{
return y[i]==y[j]&&(i+l>len?-1:y[i+l])==(j+l>len?-1:y[j+l]);
}
inline void work()
{
int i,j,k,p;m=128;
fo(i,0,m) b[i]=0;
fo(i,1,len) b[x[i]=a[i]]++;
fo(i,1,m) b[i]+=b[i-1];
for(i=len;i>=1;i--) sa[b[x[i]]--]=i;
for(k=1;k<=len;k<<=1)
{
p=0;
fo(i,len-k+1,len) y[++p]=i;
fo(i,1,len) if(sa[i]>k) y[++p]=sa[i]-k;
fo(i,0,m) b[i]=0;
fo(i,1,len) b[x[y[i]]]++;
fo(i,1,m) b[i]+=b[i-1];
for(i=len;i>=1;i--) sa[b[x[y[i]]]--]=y[i];
swap(x,y),p=2,x[sa[1]]=1;
fo(i,2,len)
x[sa[i]]=comp(sa[i-1],sa[i],k)?p-1:p++;
if(p>len) break;
m=p;
}
p=k=0;
fo(i,1,len) rank[sa[i]]=i;
for(i=1;i<=len;height[rank[i++]]=k)
for(k?k--:0,j=sa[rank[i]-1];a[i+k]==a[j+k];k++);
}
inline void init()
{
int i,j;
fo(i,1,len) st[i][0]=height[i];
fo(j,1,28)
fo(i,1,len) if((i+(1<<j)-1)<=len)
st[i][j]=min(st[i][j-1],st[i+(1<<j-1)][j-1]);
}
inline int lcp(int x,int y)
{
if(x>y) swap(x,y);
x++;
int k=0;
while(x+(1<<k+1)<=y) k++;
return min(st[x][k],st[y-(1<<k)+1][k]);
}
}A,B;
int main()
{
int i,j,l,r,L;
scanf("%d",&T);
while(T--)
{
ll ans=0;
M(f),M(g);
M(A.rank),M(A.sa),M(A.height),M(A.a);
M(B.rank),M(B.sa),M(B.height),M(B.a);
scanf("%s",s+1);
len=strlen(s+1);
fo(i,1,len) A.a[i]=s[i];
fo(i,1,len) B.a[i]=s[len-i+1];
A.work(),A.init();
B.work(),B.init();
for(i=1;i+i<=len;i++) //枚举一半长度
{
for(j=i+1;j<=len;j+=i)
if(s[j]==s[j-i])
{
int lef=min(B.lcp(B.rank[len-j+2],B.rank[len-j+i+2]),i-1);
int rig=min(A.lcp(A.rank[j],A.rank[j-i]),i);
int tmp=lef+rig-i+1;
if(tmp>=1)
{
g[j-i-lef]++,g[j-i-lef+tmp]--;
f[j+i-lef-1]++,f[j+i-lef-1+tmp]--;
}
}
}
fo(i,1,len) f[i]+=f[i-1];
fo(i,1,len) g[i]+=g[i-1];
fo(i,1,len-1) ans+=(ll)f[i]*(ll)g[i+1];
printf("%lld\n",ans);
}
return 0;
}