1 #include <bits/stdc++.h>
2 using namespace std;
3 const int maxn = 100010;
4 int rk[maxn],wb[maxn],wv[maxn],wd[maxn],lcp[maxn];
5 bool cmp(int *r,int i,int j,int k) {
6 return r[i] == r[j] && r[i+k] == r[j+k];
7 }
8 void da(int *r,int *sa,int n,int m) {
9 int i,k,p,*x = rk,*y = wb;
10 for(i = 0; i < m; ++i) wd[i] = 0;
11 for(i = 0; i < n; ++i) wd[x[i] = r[i]]++;
12 for(i = 1; i < m; ++i) wd[i] += wd[i-1];
13 for(i = n-1; i >= 0; --i) sa[--wd[x[i]]] = i;
14
15 for(p = k = 1; p < n; k <<= 1,m = p) {
16 for(p = 0,i = n-k; i < n; ++i) y[p++] = i;
17 for(i = 0; i < n; ++i) if(sa[i] >= k) y[p++] = sa[i] - k;
18 for(i = 0; i < n; ++i) wv[i] = x[y[i]];
19
20 for(i = 0; i < m; ++i) wd[i] = 0;
21 for(i = 0; i < n; ++i) wd[wv[i]]++;
22 for(i = 1; i < m; ++i) wd[i] += wd[i-1];
23 for(i = n-1; i >= 0; --i) sa[--wd[wv[i]]] = y[i];
24
25 swap(x,y);
26 x[sa[0]] = 0;
27 for(p = i = 1; i < n; ++i)
28 x[sa[i]] = cmp(y,sa[i-1],sa[i],k)?p-1:p++;
29 }
30 }
31 void calcp(int *r,int *sa,int n) {
32 for(int i = 1; i <= n; ++i) rk[sa[i]] = i;
33 int h = 0;
34 for(int i = 0; i < n; ++i) {
35 if(h > 0) h--;
36 for(int j = sa[rk[i]-1]; i+h < n && j+h < n; h++)
37 if(r[i+h] != r[j+h]) break;
38 lcp[rk[i]] = h;
39 }
40 }
41 int r[maxn],sa[maxn];
42 char str[maxn];
43 int main() {
44 int hn,x,y,cs,ret;
45 scanf("%d",&cs);
46 while(cs--) {
47 scanf("%s",str);
48 int len = strlen(str);
49 for(int i = 0; str[i]; ++i)
50 r[i] = str[i];
51 ret = r[len] = 0;
52 da(r,sa,len+1,128);
53 calcp(r,sa,len);
54 for(int i = 2; i <= len; ++i)
55 if(lcp[i] > lcp[i-1]) ret += lcp[i] - lcp[i-1];
56 printf("%d\n",ret);
57 }
58 return 0;
59 }