#define _CRT_SECURE_NO_DEPRECATE
#include <iostream>
#include <string>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <sstream>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
using namespace std;
#define _2d(a,row,col,type) {a=new type*[row]; for (int i=0;i<col;i++) a[i]=new type[col];}
#define rep(i,n) for(int i=0;i<int(n);i++)
#define repd(i,n) for(int i=n-1;i>=0;i--)
#define repi(i,a,n) for(int i=int(a);i<int(n);i++)
#define scn(a,n) {rep(i,n) cin>>a[i];}
#define scn2(a,row,col) {rep(i,row) rep(j,row) cin>>a[i][j];}
#define ll long long
#define ul unsigned long
#define ull unsigned long long
#define ld long double
#define vi vector<int>
#define vll vector<ll>
#define vl vector<long>;
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
const int MAXN = 1 << 21;
char * S;
int N, gap;
int sa[MAXN], pos[MAXN], tmp[MAXN], lcp[MAXN];
bool sufCmp(int i, int j)
{
if (pos[i] != pos[j])
return pos[i] < pos[j];
i += gap;
j += gap;
return (i < N && j < N) ? pos[i] < pos[j] : i > j;
}
void buildSA(string s)
{
N = s.length();
REP(i, N) sa[i] = i, pos[i] = s[i];
for (gap = 1;; gap *= 2)
{
sort(sa, sa + N, sufCmp);
REP(i, N - 1) tmp[i + 1] = tmp[i] + sufCmp(sa[i], sa[i + 1]);
REP(i, N) pos[sa[i]] = tmp[i];
if (tmp[N - 1] == N - 1) break;
}
}
void buildLCP(string s)
{
for (int i = 0, k = 0; i < N; ++i) if (pos[i] != N - 1)
{
for (int j = sa[pos[i] + 1]; s[i + k] == s[j + k];)
++k;
lcp[pos[i]] = k;
if (k)--k;
}
}
int main()
{
int t;
cin >> t;
string s;
while (t--){
cin >> s;
s += '$';
buildSA(s);
buildLCP(s);
int res = 0;
rep(i, s.length()){
res += sa[i] - lcp[i-1];
}
cout << res << endl;
}
return 0;
}
I2RlZmluZSBfQ1JUX1NFQ1VSRV9OT19ERVBSRUNBVEUKI2luY2x1ZGUgPGlvc3Ry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