#include<bits/stdc++.h>
using namespace std;
int P[1003][10],LCP[1003],Pow[12],SA[1003];
struct tuplee
{
int index,first,second;
}L[1003];
bool comp(tuplee &a,tuplee &b)
{
if(a.first==b.first)
return (a.second < b.second);
else
return (a.first < b.first);
}
int build_suffix(string s)
{
int n=s.size();
int i,j;
for(i=0;i<n;i++)
P[i][0]=(int)s[i];
for(i=1;Pow[i-1]<n;i++)
{
for(j=0;j<n;j++)
{
L[j].index=j;
L[j].first=P[j][i-1];
L[j].second=((j+Pow[i-1]<n)?P[j+Pow[i-1]][i-1]:-1);
}
sort(L,L+n,comp);
for(j=0;j<n;j++)
P[L[j].index][i]=((j>0 && L[j].first==L[j-1].first && L[j].second==L[j-1].second)?P[L[j-1].index][i]:j);
}
i--;
for(j=0;j<n;j++)
SA[P[j][i]]=j;
return i;
}
int lcp(int x,int y,int step,int n)
{
int i;
int len=0;
for(i=step;i>=0;i--)
{
if(P[x][i]==P[y][i] && x<n && y<n)
{
len+=Pow[i];
x=x+Pow[i];
y=y+Pow[i];
}
}
return len;
}
int solve(int n,int step)
{
int i,ans=0;
for(i=1;i<n;i++)
ans+=lcp(SA[i],SA[i-1],step,n);
ans=(n*(n+1))/2 -(ans);
return ans;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin>>t;
int i;
Pow[0]=1;
for(i=1;i<=10;i++)
Pow[i]=2*Pow[i-1];
while(t--)
{
string s;
cin>>s;
int n=s.size();
int step=build_suffix(s);
cout<<solve(n,step)<<endl;
}
return 0;
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKaW50IFBbMTAwM11bMTBdLExDUFsxMDAzXSxQb3dbMTJdLFNBWzEwMDNdOwoKc3RydWN0IHR1cGxlZQp7CiBpbnQgaW5kZXgsZmlyc3Qsc2Vjb25kOyAgICAKfUxbMTAwM107Cgpib29sIGNvbXAodHVwbGVlICZhLHR1cGxlZSAmYikKewogaWYoYS5maXJzdD09Yi5maXJzdCkKIHJldHVybiAoYS5zZWNvbmQgPCBiLnNlY29uZCk7CiBlbHNlCiByZXR1cm4gKGEuZmlyc3QgPCBiLmZpcnN0KTsKfQoKaW50IGJ1aWxkX3N1ZmZpeChzdHJpbmcgcykKewogaW50IG49cy5zaXplKCk7ICAgIAogaW50IGksajsKIAogZm9yKGk9MDtpPG47aSsrKQogUFtpXVswXT0oaW50KXNbaV07CiAKIGZvcihpPTE7UG93W2ktMV08bjtpKyspCiB7CiAgIGZvcihqPTA7ajxuO2orKykKICAgewogICAgIExbal0uaW5kZXg9ajsKICAgICBMW2pdLmZpcnN0PVBbal1baS0xXTsKICAgICBMW2pdLnNlY29uZD0oKGorUG93W2ktMV08bik/UFtqK1Bvd1tpLTFdXVtpLTFdOi0xKTsKICAgfQogICAgCiAgIHNvcnQoTCxMK24sY29tcCk7CiAgIAogICBmb3Ioaj0wO2o8bjtqKyspCiAgUFtMW2pdLmluZGV4XVtpXT0oKGo+MCAmJiBMW2pdLmZpcnN0PT1MW2otMV0uZmlyc3QgJiYgTFtqXS5zZWNvbmQ9PUxbai0xXS5zZWNvbmQpP1BbTFtqLTFdLmluZGV4XVtpXTpqKTsgCgogfQogCiBpLS07CiAKIGZvcihqPTA7ajxuO2orKykKIFNBW1Bbal1baV1dPWo7CiAKIHJldHVybiBpOwp9CgppbnQgbGNwKGludCB4LGludCB5LGludCBzdGVwLGludCBuKQp7IAogIGludCBpOwogIGludCBsZW49MDsKICBmb3IoaT1zdGVwO2k+PTA7aS0tKQogIHsKICAgIGlmKFBbeF1baV09PVBbeV1baV0gJiYgeDxuICYmIHk8bikgIAogICAgeyAKICAgICBsZW4rPVBvd1tpXTsKICAgICB4PXgrUG93W2ldOwogICAgIHk9eStQb3dbaV07CiAgICB9ICAKICAgICAgCiAgfQogICAgcmV0dXJuIGxlbjsgICAKfQoKaW50IHNvbHZlKGludCBuLGludCBzdGVwKQp7IAogaW50IGksYW5zPTA7ICAgIAogCiBmb3IoaT0xO2k8bjtpKyspCiBhbnMrPWxjcChTQVtpXSxTQVtpLTFdLHN0ZXAsbik7CiAKIGFucz0obioobisxKSkvMiAtKGFucyk7CiAKIHJldHVybiBhbnM7Cn0KCmludCBtYWluKCkKewogIGlvc19iYXNlOjpzeW5jX3dpdGhfc3RkaW8oMCk7CiAgY2luLnRpZSgwKTsKICBjb3V0LnRpZSgwKTsKICAKICBpbnQgdDsKICBjaW4+PnQ7CiAgaW50IGk7CiAgUG93WzBdPTE7CiAgZm9yKGk9MTtpPD0xMDtpKyspCiAgUG93W2ldPTIqUG93W2ktMV07CiAgCiAgd2hpbGUodC0tKQogIHsgCiAgICBzdHJpbmcgczsKICAgIGNpbj4+czsKICAgIGludCBuPXMuc2l6ZSgpOwogICAgaW50IHN0ZXA9YnVpbGRfc3VmZml4KHMpOwogICAgY291dDw8c29sdmUobixzdGVwKTw8ZW5kbDsKICB9CiAgICAKIHJldHVybiAwOyAgICAKfQ==