#include<cstdio>
#include<cstring>
#define N 55000
char a[N];
int n,m,k,T,tmp[N],rank[N<<1],rank1[N],sa[N],height[N],count[N];
inline int max(int x,int y){
return x>y?x:y;
}
int main(){
freopen("spoj694.in","r",stdin);
freopen("spoj694.out","w",stdout);
scanf("%d",&T);
while (T--){
scanf("%s",a+1);n=strlen(a+1);m=0;
// for (int i=1;i<=n;++i) printf("%c,a[i]);printf("\n");
memset(count,0,sizeof (count));
memset(rank,0,sizeof(rank));
for (int i=1;i<=n;++i) count[a[i]]=1;
for (int i=1;i<=255;++i) count[i]+=count[i-1],m=max(m,count[i]);
for (int i=1;i<=n;++i) rank[i]=count[a[i]];
k=0;
for (int p=1;k!=n;p<<=1,m=k){
for (int i=1;i<=m;++i) count[i]=0;
for (int i=1;i<=n;++i) count[rank[i+p]]++;
for (int i=1;i<=m;++i) count[i]+=count[i-1];
for (int i=n;i>=1;--i) tmp[count[rank[i+p]]--]=i;
for (int i=1;i<=m;++i) count[i]=0;
for (int i=1;i<=n;++i) count[rank[i]]++;
for (int i=1;i<=m;++i) count[i]+=count[i-1];
for (int i=n;i>=1;--i) sa[count[rank[tmp[i]]]--]=tmp[i];
memcpy(rank1,rank,sizeof(rank)>>1);
rank[sa[1]]=k=1;
for (int i=2;i<=n;++i){
if (rank1[sa[i]+p]!=rank1[sa[i-1]+p]||rank1[sa[i]]!=rank1[sa[i-1]])++k;
rank[sa[i]]=k;
}
}
// for (int i=1;i<=n;++i) printf("%d,rank[i]);printf("\n");
k=0;
for (int i=1;i<=n;++i){
if (rank[i]==1) continue;
k=k==0?0:k-1;
while (a[i+k]==a[sa[rank[i]-1]+k]) ++k;
height[rank[i]]=k;
}
//for (int i=1;i<=n;++i) printf("%d,height[i]);printf("\n");
int ans=0;
for (int i=1;i<=n;++i) ans+=i;
for (int i=1;i<=n;++i) ans-=height[i];
printf("%d\n",ans);
}
return 0;
}