#include <iostream>
#include <vector>
#include <algorithm>
#include <stdio.h>
#include <sstream>
#include <string>
#include <map>
#include <set>
#include <stdlib.h>
#include <cmath>
#include <math.h>
#include <fstream>
#include <bitset>
#include <time.h>
#define int long long
using namespace std;
int counter;
int go[200001][26];
int last;
int suf[200001], len[200001], ans[200001];
int reb;
map<int, int> num;
string id = "abcdefghijklmnopqrstuvwxyz";
void add(int number){
    int newlast = counter; len[newlast] = len[last] + 1; int p = last; counter++;
    while (p!=-1 && go[p][number] == -1){
        go[p][number] = newlast;
        reb ++;
        p = suf[p];
    }
    if (p == -1){
        suf[newlast] = 0;
    }
    else{
        int q = go[p][number];
        if (len[q] == len[p] + 1){
            suf[newlast] = q;
        }
        else{
            int r = counter; counter ++;
            for (int i=0;i<26;i++){
                go[r][i] = go[q][i];
                if (go[r][i] != -1) reb++;
            }
            suf[r] = suf[q];
            suf[q] = r;
            suf[newlast] = r;
            len[r] = len[p] + 1;
            while (p!=-1 && go[p][number] == q){
                go[p][number] = r;
                p = suf[p];
            }
        }
    }
    last = newlast;
}
void dfs(int vertex){
    int dost = 0;
    if (num.count(vertex)) dost = num[vertex];
    for (int i=0;i<26;i++){
        int to = go[vertex][i];
        if (to != -1 && ans[to] == -1) dfs(to);
        if (to != -1) dost = max(dost, ans[to]);
    }
    ans[vertex] = dost;
}
int32_t main()
{
    int m;
    cin >> m;
    for (int i=0;i<m;i++){
        string s;
        cin >> s;
        for (int j=0;j<200001;j++){
            ans[j] = -1;
            suf[j] = -1;
            for (int k=0;k<26;k++){
                go[j][k] = -1;
            }
        }
        len[0] = 0;
        counter = 1;
        last = 0;
        for (int j=0;j<s.size();j++){
            for (int k=0;k<26;k++){
                if (id[k] == s[j]) add(k);
            }
        }
        int length = 1;
        while (last != -1){
            num[last] = length;
            length ++;
            last = suf[last];
        }
        reb = 0;
        dfs(0);
        vector<int> in;
        vector<int> zero, sz;
        for (int i=0;i<counter;i++){
            in.push_back(0);
            sz.push_back(0);
        }
        sz[0] = 1;
        zero.push_back(0);
        for (int i=0;i<counter;i++){
            for (int j=0;j<26;j++){
                if (go[i][j] != -1) in[go[i][j]] ++;
            }
        }
        while (zero.size() > 0){
            int v = zero.back();
            if (v != 0) reb += ans[v] * ans[v] * sz[v];
            zero.pop_back();
            for (int i=0;i<26;i++){
                if (go[v][i] != -1) {
                in[go[v][i]] --;
                sz[go[v][i]] += sz[v];
                if (in[go[v][i]] == 0){
                    zero.push_back(go[v][i]);
                }
                }
            }
        }
        cout << reb << endl;
    }
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <stdio.h>
#include <sstream>
#include <string>
#include <map>
#include <set>
#include <stdlib.h>
#include <cmath>
#include <math.h>
#include <fstream>
#include <bitset>
#include <time.h>
#define int long long
using namespace std;
int counter;
int go[200001][26];
int last;
int suf[200001], len[200001], ans[200001];
int reb;
map<int, int> num;
string id = "abcdefghijklmnopqrstuvwxyz";
void add(int number){
    int newlast = counter; len[newlast] = len[last] + 1; int p = last; counter++;
    while (p!=-1 && go[p][number] == -1){
        go[p][number] = newlast;
        reb ++;
        p = suf[p];
    }
    if (p == -1){
        suf[newlast] = 0;
    }
    else{
        int q = go[p][number];
        if (len[q] == len[p] + 1){
            suf[newlast] = q;
        }
        else{
            int r = counter; counter ++;
            for (int i=0;i<26;i++){
                go[r][i] = go[q][i];
                if (go[r][i] != -1) reb++;
            }
            suf[r] = suf[q];
            suf[q] = r;
            suf[newlast] = r;
            len[r] = len[p] + 1;
            while (p!=-1 && go[p][number] == q){
                go[p][number] = r;
                p = suf[p];
            }
        }
    }
    last = newlast;
}
void dfs(int vertex){
    int dost = 0;
    if (num.count(vertex)) dost = num[vertex];
    for (int i=0;i<26;i++){
        int to = go[vertex][i];
        if (to != -1 && ans[to] == -1) dfs(to);
        if (to != -1) dost = max(dost, ans[to]);
    }
    ans[vertex] = dost;
}
int32_t main()
{
    int m;
    cin >> m;
    for (int i=0;i<m;i++){
        string s;
        cin >> s;
        for (int j=0;j<200001;j++){
            ans[j] = -1;
            suf[j] = -1;
            len[j] = -1;
            for (int k=0;k<26;k++){
                go[j][k] = -1;
            }
        }
        len[0] = 0;
        counter = 1;
        last = 0;
        for (int j=0;j<s.size();j++){
            for (int k=0;k<26;k++){
                if (id[k] == s[j]) add(k);
            }
        }
        int length = 1;
        num.clear();
        while (last != -1){
            num[last] = length;
            length ++;
            last = suf[last];
        }
        reb = 0;
        dfs(0);
        vector<int> in;
        vector<int> zero, sz;
        for (int i=0;i<counter;i++){
            in.push_back(0);
            sz.push_back(0);
        }
        sz[0] = 1;
        zero.push_back(0);
        for (int i=0;i<counter;i++){
            for (int j=0;j<26;j++){
                if (go[i][j] != -1) in[go[i][j]] ++;
            }
        }
        while (zero.size() > 0){
            int v = zero.back();
            if (v != 0) reb += ans[v] * ans[v] * sz[v];
            zero.pop_back();
            for (int i=0;i<26;i++){
                if (go[v][i] != -1) {
                in[go[v][i]] --;
                sz[go[v][i]] += sz[v];
                if (in[go[v][i]] == 0){
                    zero.push_back(go[v][i]);
                }
                }
            }
        }
        cout << reb << endl;
    }
    return 0;
}


#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <vector>
#include <queue>
#include <time.h>
#define eps 1e-7
#define INF 0x3f3f3f3f
#define MOD 1000000007
#define rep0(j,n) for(int j=0;j<n;++j)
#define rep1(j,n) for(int j=1;j<=n;++j)
#define pb push_back
#define set0(n) memset(n,0,sizeof(n))
#define ll long long
#define ull unsigned long long
#define iter(i,v) for(edge *i=head[v];i;i=i->nxt)
#define max(a,b) (a>b?a:b)
#define min(a,b) (a<b?a:b)
#define print_runtime printf("Running time:%.3lfs\n",double(clock())/1000.0)
#define TO(j) printf(#j": %d\n",j);
//#define OJ
using namespace std;
const int MAXINT = 100010;
const int MAXNODE = 100010;
const int MAXEDGE = 2*MAXNODE;
char BUF,*buf;
int read(){
    char c=getchar();int f=1,x=0;
    while(!isdigit(c)){if(c=='-') f=-1;c=getchar();}
    while(isdigit(c)){x=x*10+c-'0';c=getchar();}
    return f*x;
}
char get_ch(){
    char c=getchar();
    while(!isalpha(c)) c=getchar();
    return c;
}
//------------------- Head Files ----------------------//
char s[MAXINT];
struct cond{
	cond *c[26],*fa;
	int maxl,r,cnt;
};
struct SAM{
	cond *root,*last;
	cond mp[MAXINT*5];
	int ct[MAXINT],srt[MAXINT*5];
	int cnt;
	cond * newnode(int maxl){
		cond *p = &mp[cnt++];
		memset(p->c,0,sizeof(p->c));
		p->fa=NULL;
		p->maxl=maxl;
		p->cnt=p->r=0;
		return p;
	}
	SAM(){
		cnt=0;
		root = newnode(0);
		last=root;
	}
	void append(int w){
		cond *p=last;
		cond *np=newnode(p->maxl+1);
		np->r=1;
		while(p&&!p->c[w]) p->c[w]=np,p=p->fa;
		if(!p) np->fa=root;
		else{
			cond *q=p->c[w];
			if(q->maxl==p->maxl+1) np->fa=q;
			else{
				cond *nq = newnode(p->maxl+1);
				memcpy(nq->c,q->c,sizeof(q->c));
				nq->fa=q->fa;
				q->fa=nq;np->fa=nq;
				while(p&&p->c[w]==q) p->c[w]=nq,p=p->fa;
			}
		}
		last=np;
	}
	ll calc(){
		memset(ct,0,sizeof(ct));
		ll ans=0;
		rep1(i,cnt-1) ct[mp[i].maxl]++;
		rep1(i,MAXINT-1) ct[i]+=ct[i-1];
		rep1(i,cnt-1) srt[ct[mp[i].maxl]--]=i;
		for(int i=cnt-1;i>=1;i--) {
			mp[srt[i]].cnt+=mp[srt[i]].r;
			mp[srt[i]].fa->cnt+=mp[srt[i]].cnt;
		}
		rep1(i,cnt-1) ans+=(ll)(mp[i].maxl-mp[i].fa->maxl)*mp[i].cnt*mp[i].cnt;
		cnt=0;
		root = newnode(0);
		last=root;
		return ans;
	}
}sam;
void get_input();
void work();
int main() {
	int T=read();
	while(T--){
		get_input();
    	work();
	}
    return 0;
}
void work(){
	printf("%lld\n",sam.calc());
}
void get_input(){
    scanf("%s",s);
    int l =strlen(s);
    rep0(i,l) sam.append(s[i]-'a');
}


#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <vector>
#include <queue>
#include <time.h>
#define eps 1e-7
#define INF 0x3f3f3f3f
#define MOD 1000000007
#define rep0(j,n) for(int j=0;j<n;++j)
#define rep1(j,n) for(int j=1;j<=n;++j)
#define pb push_back
#define set0(n) memset(n,0,sizeof(n))
#define ll long long
#define ull unsigned long long
#define iter(i,v) for(edge *i=head[v];i;i=i->nxt)
#define max(a,b) (a>b?a:b)
#define min(a,b) (a<b?a:b)
#define print_runtime printf("Running time:%.3lfs\n",double(clock())/1000.0)
#define TO(j) printf(#j": %d\n",j);
//#define OJ
using namespace std;
const int MAXINT = 100010;
const int MAXNODE = 100010;
const int MAXEDGE = 2*MAXNODE;
char BUF,*buf;
int read(){
    char c=getchar();int f=1,x=0;
    while(!isdigit(c)){if(c=='-') f=-1;c=getchar();}
    while(isdigit(c)){x=x*10+c-'0';c=getchar();}
    return f*x;
}
char get_ch(){
    char c=getchar();
    while(!isalpha(c)) c=getchar();
    return c;
}
//------------------- Head Files ----------------------//
char s[MAXINT];
struct cond{
	cond *c[26],*fa;
	int maxl,r,cnt;
};
struct SAM{
	cond *root,*last;
	cond mp[MAXINT];
	int ct[MAXINT],srt[MAXINT];
	int cnt;
	cond * newnode(int maxl){
		cond *p = &mp[cnt++];
		memset(p->c,0,sizeof(p->c));
		p->fa=NULL;
		p->maxl=maxl;
		p->cnt=p->r=0;
		return p;
	}
	SAM(){
		cnt=0;
		root = newnode(0);
		last=root;
	}
	void append(int w){
		cond *p=last;
		cond *np=newnode(p->maxl+1);
		np->r=1;
		while(p&&!p->c[w]) p->c[w]=np,p=p->fa;
		if(!p) np->fa=root;
		else{
			cond *q=p->c[w];
			if(q->maxl==p->maxl+1) np->fa=q;
			else{
				cond *nq = newnode(p->maxl+1);
				memcpy(nq->c,q->c,sizeof(q->c));
				nq->fa=q->fa;
				q->fa=nq;np->fa=nq;
				while(p&&p->c[w]==q) p->c[w]=nq,p=p->fa;
			}
		}
		last=np;
	}
	ll calc(){
		memset(ct,0,sizeof(ct));
		ll ans=0;
		rep1(i,cnt-1) ct[mp[i].maxl]++;
		rep1(i,MAXINT-1) ct[i]+=ct[i-1];
		rep1(i,cnt-1) srt[ct[mp[i].maxl]--]=i;
		for(int i=cnt-1;i>=1;i--) {
			mp[srt[i]].cnt+=mp[srt[i]].r;
			mp[srt[i]].fa->cnt+=mp[srt[i]].cnt;
		}
		rep1(i,cnt-1) ans+=(mp[i].maxl-mp[i].fa->maxl)*mp[i].cnt*mp[i].cnt;
		cnt=0;
		root = newnode(0);
		last=root;
		return ans;
	}
}sam;
void get_input();
void work();
int main() {
	int T=read();
	while(T--){
		get_input();
    	work();
	}
    return 0;
}
void work(){
	printf("%lld\n",sam.calc());
}
void get_input(){
    scanf("%s",s);
    int l =strlen(s);
    rep0(i,l) sam.append(s[i]-'a');
}


#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <vector>
#include <queue>
#include <time.h>
#define eps 1e-7
#define INF 0x3f3f3f3f
#define MOD 1000000007
#define rep0(j,n) for(int j=0;j<n;++j)
#define rep1(j,n) for(int j=1;j<=n;++j)
#define pb push_back
#define set0(n) memset(n,0,sizeof(n))
#define ll long long
#define ull unsigned long long
#define iter(i,v) for(edge *i=head[v];i;i=i->nxt)
#define max(a,b) (a>b?a:b)
#define min(a,b) (a<b?a:b)
#define print_runtime printf("Running time:%.3lfs\n",double(clock())/1000.0)
#define TO(j) printf(#j": %d\n",j);
//#define OJ
using namespace std;
const int MAXINT = 100010;
const int MAXNODE = 100010;
const int MAXEDGE = 2*MAXNODE;
char BUF,*buf;
int read(){
    char c=getchar();int f=1,x=0;
    while(!isdigit(c)){if(c=='-') f=-1;c=getchar();}
    while(isdigit(c)){x=x*10+c-'0';c=getchar();}
    return f*x;
}
char get_ch(){
    char c=getchar();
    while(!isalpha(c)) c=getchar();
    return c;
}
//------------------- Head Files ----------------------//
char s[MAXINT];
struct cond{
	cond *c[26],*fa;
	int maxl,r,cnt;
};
struct SAM{
	cond *root,*last;
	cond mp[MAXINT*4];
	int ct[MAXINT],srt[MAXINT];
	int cnt;
	cond * newnode(int maxl){
		cond *p = &mp[cnt++];
		memset(p->c,0,sizeof(p->c));
		p->fa=NULL;
		p->maxl=maxl;
		p->cnt=p->r=0;
		return p;
	}
	SAM(){
		cnt=0;
		root = newnode(0);
		last=root;
	}
	void append(int w){
		cond *p=last;
		cond *np=newnode(p->maxl+1);
		np->r=1;
		while(p&&!p->c[w]) p->c[w]=np,p=p->fa;
		if(!p) np->fa=root;
		else{
			cond *q=p->c[w];
			if(q->maxl==p->maxl+1) np->fa=q;
			else{
				cond *nq = newnode(p->maxl+1);
				memcpy(nq->c,q->c,sizeof(q->c));
				nq->fa=q->fa;
				q->fa=nq;np->fa=nq;
				while(p&&p->c[w]==q) p->c[w]=nq,p=p->fa;
			}
		}
		last=np;
	}
	ll calc(){
		memset(ct,0,sizeof(ct));
		ll ans=0;
		rep1(i,cnt-1) ct[mp[i].maxl]++;
		rep1(i,MAXINT-1) ct[i]+=ct[i-1];
		rep1(i,cnt-1) srt[ct[mp[i].maxl]--]=i;
		for(int i=cnt-1;i>=1;i--) {
			mp[srt[i]].cnt+=mp[srt[i]].r;
			mp[srt[i]].fa->cnt+=mp[srt[i]].cnt;
		}
		rep1(i,cnt-1) ans+=(ll)(mp[i].maxl-mp[i].fa->maxl)*mp[i].cnt*mp[i].cnt;
		cnt=0;
		root = newnode(0);
		last=root;
		return ans;
	}
}sam;
void get_input();
void work();
int main() {
	int T=read();
	while(T--){
		get_input();
    	work();
	}
    return 0;
}
void work(){
	printf("%lld\n",sam.calc());
}
void get_input(){
    scanf("%s",s);
    int l =strlen(s);
    rep0(i,l) sam.append(s[i]-'a');
}


#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <cstring>
#define SIZE 100005

using namespace std;
typedef long long int ll;
typedef pair <int,int> P;
typedef pair <P,int> PP;

char str[SIZE];
int rank[SIZE],sa[SIZE],lcp[SIZE];
PP tmp[SIZE];
P st[SIZE];

void solve()
{
	scanf("%s",&str);
	int n=strlen(str);
	for(int i=0;i<n;i++) rank[i]=str[i]-'a';
	for(int k=1;k<=n;k<<=1)
	{
		for(int i=0;i<n;i++)
		{
			tmp[i]=PP(P(rank[i],i+k<n?rank[i+k]:-1),i);
		}
		sort(tmp,tmp+n);
		for(int i=0;i<n;)
		{
			int f=i;
			for(;i<n&&tmp[i].first==tmp[f].first;i++) rank[tmp[i].second]=f;
		}
	}
	for(int i=0;i<n;i++) sa[rank[i]]=i;
	int H=0;
	for(int i=0;i<n;i++)
	{
		if(H>0) H--;
		if(rank[i]==n-1) continue;
		int to=sa[rank[i]+1];
		while(i+H<n&&to+H<n&&str[i+H]==str[to+H]) H++;
		lcp[rank[i]]=H;
	}
	int sz=0;
	st[sz++]=P(n-sa[0],0);
	ll ret=0;
	for(int i=1;i<n;i++)
	{
		int back=lcp[i-1];
		int last=i-1;
		while(sz>0&&st[sz-1].first>=back)
		{
			int nxt=sz==1?0:st[sz-2].first;
			nxt=max(nxt,back);
			ret+=(ll) (st[sz-1].first-nxt)*(ll) (i-st[sz-1].second)*(ll) (i-st[sz-1].second);
			last=st[sz-1].second;
			sz--;
		}
		st[sz++]=P(back,last);
		if(back<n-sa[i]) st[sz++]=P(n-sa[i],i);
	}
	while(sz>0)
	{
		int nxt=sz==1?0:st[sz-2].first;
		ret+=(ll) (st[sz-1].first-nxt)*(ll) (n-st[sz-1].second)*(ll) (n-st[sz-1].second);
		sz--;
	}
	printf("%lld\n",ret);
}
int main()
{
	int T;
	scanf("%d",&T);
	while(T--)
	{
		solve();
	}
	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <cstring>
#define SIZE 100005

using namespace std;
typedef long long int ll;
typedef pair <int,int> P;
typedef pair <P,int> PP;

char str[SIZE];
int rnk[SIZE],sa[SIZE],lcp[SIZE];
PP tmp[SIZE];
P st[SIZE];

void solve()
{
	scanf("%s",&str);
	int n=strlen(str);
	for(int i=0;i<n;i++) rnk[i]=str[i]-'a';
	for(int k=1;k<=n;k<<=1)
	{
		for(int i=0;i<n;i++)
		{
			tmp[i]=PP(P(rnk[i],i+k<n?rnk[i+k]:-1),i);
		}
		sort(tmp,tmp+n);
		for(int i=0;i<n;)
		{
			int f=i;
			for(;i<n&&tmp[i].first==tmp[f].first;i++) rnk[tmp[i].second]=f;
		}
	}
	for(int i=0;i<n;i++) sa[rnk[i]]=i;
	int H=0;
	for(int i=0;i<n;i++)
	{
		if(H>0) H--;
		if(rnk[i]==n-1) continue;
		int to=sa[rnk[i]+1];
		while(i+H<n&&to+H<n&&str[i+H]==str[to+H]) H++;
		lcp[rnk[i]]=H;
	}
	int sz=0;
	st[sz++]=P(n-sa[0],0);
	ll ret=0;
	for(int i=1;i<n;i++)
	{
		int back=lcp[i-1];
		int last=i-1;
		while(sz>0&&st[sz-1].first>=back)
		{
			int nxt=sz==1?0:st[sz-2].first;
			nxt=max(nxt,back);
			ret+=(ll) (st[sz-1].first-nxt)*(ll) (i-st[sz-1].second)*(ll) (i-st[sz-1].second);
			last=st[sz-1].second;
			sz--;
		}
		st[sz++]=P(back,last);
		if(back<n-sa[i]) st[sz++]=P(n-sa[i],i);
	}
	while(sz>0)
	{
		int nxt=sz==1?0:st[sz-2].first;
		ret+=(ll) (st[sz-1].first-nxt)*(ll) (n-st[sz-1].second)*(ll) (n-st[sz-1].second);
		sz--;
	}
	printf("%lld\n",ret);
}
int main()
{
	int T;
	scanf("%d",&T);
	while(T--)
	{
		solve();
	}
	return 0;
}


#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// #include <ext/pb_ds/detail/standard_policies.hpp>

using namespace std;
// using namespace __gnu_pbds;
// using namespace __gnu_cxx;

typedef long long ll;
typedef unsigned int ul;
typedef unsigned long long ull;
typedef vector <int> vi;
typedef map<int, vector<int> > mvii;
typedef map<int, int> mii;
typedef queue <int> qi;
typedef vector <string> vs;
typedef pair <int, int> pii;
typedef vector<pii > vpii;

// Order Statistic Tree

/* Special functions:

        find_by_order(k) --> returns iterator to the kth largest element counting from 0
        order_of_key(val) --> returns the number of items in a set that are strictly smaller than our item
*/

// typedef tree<
// int,
// null_type,
// less<int>,
// rb_tree_tag,
// tree_order_statistics_node_update>
// ordered_set;

#define MP make_pair
#define SORT(a) sort (a.begin(), a.end())
#define REVERSE(a) reverse (a.begin(), a.end())
#define ALL(a) a.begin(), a.end()
#define PI acos(-1)
#define ms(x,y) memset (x, y, sizeof (x))
#define inf 1e9
#define INF 1e16
#define pb push_back
#define MAX 100005
#define debug(a,b) cout<<a<<": "<<b<<endl
#define Debug cout<<"Reached here"<<endl
#define prnt(a) cout<<a<<"\n"
#define mod 1000000007LL
#define FOR(i,a,b) for (int i=(a); i<(b); i++)
#define FORr(i,a,b) for (int i=(a); i>=(b); i--)
#define itrALL(c,itr) for(__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++)
#define lc ((node)<<1)
#define rc ((node)<<1|1)
#define VecPrnt(v) FOR(J,0,v.size()) cout<<v[J]<<" "; cout<<endl
#define endl "\n"
#define PrintPair(x) cout<<x.first<<" "<<x.second<<endl
#define EPS 1e-9
#define ArrPrint(a,st,en) for(int J=st; J<=en; J++) cout<<a[J]<<" "; cout<<endl;

/* Direction Array */

// int fx[]={1,-1,0,0};
// int fy[]={0,0,1,-1};
// int fx[]={0,0,1,-1,-1,1,-1,1};
// int fy[]={-1,1,0,0,1,1,-1,-1};

template <class T> inline T bigmod(T p, T e, T M)
{
      ll ret = 1;
      for (; e > 0; e >>= 1)
      {
            if (e & 1) ret = (ret * p) % M;
            p = (p * p) % M;
      } return (T)ret;
}

template <class T> inline T gcd(T a, T b) {if (b == 0)return a; return gcd(b, a % b);}
template <class T> inline T modinverse(T a, T M) {return bigmod(a, M - 2, M);}
template <class T> inline T lcm(T a, T b) {a = abs(a); b = abs(b); return (a / gcd(a, b)) * b;}
template <class T, class X> inline bool getbit(T a, X i) { T t = 1; return ((a & (t << i)) > 0);}
template <class T, class X> inline T setbit(T a, X i) { T t = 1; return (a | (t << i)); }
template <class T, class X> inline T resetbit(T a, X i) { T t = 1; return (a & (~(t << i)));}

inline ll getnum()
{
      char c = getchar();
      ll num, sign = 1;
      for (; c < '0' || c > '9'; c = getchar())if (c == '-')sign = -1;
      for (num = 0; c >= '0' && c <= '9';)
      {
            c -= '0';
            num = num * 10 + c;
            c = getchar();
      }
      return num * sign;
}

inline ll power(ll a, ll b)
{
      ll multiply = 1;
      FOR(i, 0, b)
      {
            multiply *= a;
      }
      return multiply;
}

/***************** END OF HEADER *****************/

const int N = 1e5+5;
int wa[N],wb[N],wv[N],wc[N];
int r[N],sa[N],rak[N], height[N];

int cmp(int *r,int a,int b,int l)
{
      return r[a] == r[b] && r[a+l] == r[b+l];
}

void da(int *r,int *sa,int n,int m)
{
      int i,j,p,*x=wa,*y=wb,*t;
      for( i=0;i<m;i++) wc[i]=0;
      for( i=0;i<n;i++) wc[x[i]=r[i]] ++;
      for( i=1;i<m;i++) wc[i] += wc[i-1];
      for( i= n-1;i>=0;i--)sa[--wc[x[i]]] = i;
      for( j= 1,p=1;p<n;j*=2,m=p){
            for(p=0,i=n-j;i<n;i++)y[p++] = i;
            for(i=0;i<n;i++)if(sa[i] >= j) y[p++] = sa[i] - j;
            for(i=0;i<n;i++)wv[i] = x[y[i]];
            for(i=0;i<m;i++) wc[i] = 0;
            for(i=0;i<n;i++) wc[wv[i]] ++;
            for(i=1;i<m;i++) wc[i] += wc[i-1];
            for(i=n-1;i>=0;i--) sa[--wc[wv[i]]] = y[i];
            for(t=x,x=y,y=t,p=1,x[sa[0]] = 0,i=1;i<n;i++) x[sa[i]]= cmp(y,sa[i-1],sa[i],j) ? p-1:p++;

      }
}

void calheight(int *r,int *sa,int n)
{

      int i,j,k=0;
      for(i=1;i<=n;i++) rak[sa[i]] = i;
      for(i=0;i<n;height[rak[i++]] = k ) {

            for(k?k--:0, j=sa[rak[i]-1] ; r[i+k] == r[j+k] ; k ++) ;
      }

}

int dp[N][22];

void initRMQ(int n)
{
      for(int i= 1;i<=n;i++) dp[i][0] = height[i];
      for(int j= 1; (1<<j) <= n; j ++ ){
            for(int i = 1; i + (1<<j) - 1 <= n ; i ++ ) {
                  dp[i][j] = min(dp[i][j-1] , dp[i + (1<<(j-1))][j-1]);
            }
      }

}
int askRMQ(int L,int R)
{
      int k = 0;
      while((1<<(k+1)) <= R-L+1) k++;
      return min(dp[L][k], dp[R - (1<<k) + 1][k]);
}

int len, L[N], R[N];
char s[N];
vi occ[N];

void clear()
{
	FOR(i,0,len+1) occ[i].clear();
}

void simulate(stack<pii> &st, int idx, int *aux, bool flag)
{
	while(!st.empty() && st.top().first>=height[idx])
		st.pop();

	if(!st.empty()) aux[idx]=st.top().second;
	else
	{
		if(flag) aux[idx]=1;
		else aux[idx]=len+1;
	}

	st.push({height[idx],idx});
}

void calc(bool flag)
{
	stack<pii> st;
	if(flag)
	{
		FOR(i,2,len+1) simulate(st,i,L,1);
	}
	else
	{
		FORr(i,len,2) simulate(st,i,R,0);
	}
}

void buildSA()
{
	len=strlen(s);
	for(int i=0; i<len; i++)
		r[i]=s[i]-'a'+1;
	r[len]=0;

	da(r,sa,len+1,27);
	calheight(r,sa,len);

	// FOR(i,1,len+1) cout<<sa[i]<<" "<<s+sa[i]<<endl;
	// FOR(i,2,len+1) cout<<height[i]<<" "; cout<<endl;

	FOR(i,2,len+1) occ[height[i]].pb(i);

	calc(1); calc(0);

	// FOR(i,2,len+1) cout<<L[i]<<" "; cout<<endl;
	// FOR(i,2,len+1) cout<<R[i]<<" "; cout<<endl;
}

void solve(ll &t, ll &ans)
{
	FOR(i,1,len+1)
	{
		int ptr=0;

		FOR(j,0,occ[i].size())
		{
			int idx=occ[i][j], val=i;
			// cout<<"for height: "<<i<<" index: "<<idx<<endl;
			if(idx<ptr) continue;
			int lptr=L[idx], rptr=R[idx];
			int minv=inf;
			if(lptr!=1) minv=min(minv,i-height[lptr]);
			if(rptr!=len+1) minv=min(minv,i-height[rptr]);

			val=min(val,minv);

			ll x=rptr-lptr;
			ans+=val*x*x;

			t-=val*x;

			ptr=rptr;
		}
	}
}

int main()
{
      // ios_base::sync_with_stdio(0);
      // cin.tie(NULL); cout.tie(NULL);
      // freopen("in.txt","r",stdin);

      int test, cases = 1;

      scanf("%d", &test);

      while(test--)
      {
      	scanf("%s", s);

      	buildSA();
      	ll total=((ll)len*(len+1))/2, ans=0;
      	solve(total,ans);

      	// cout<<total<<" "<<ans<<endl;
      	prnt(ans+total);
      	clear();
      }

      return 0;
}


#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// #include <ext/pb_ds/detail/standard_policies.hpp>

using namespace std;
// using namespace __gnu_pbds;
// using namespace __gnu_cxx;

typedef long long ll;
typedef unsigned int ul;
typedef unsigned long long ull;
typedef vector <int> vi;
typedef map<int, vector<int> > mvii;
typedef map<int, int> mii;
typedef queue <int> qi;
typedef vector <string> vs;
typedef pair <int, int> pii;
typedef vector<pii > vpii;

// Order Statistic Tree

/* Special functions:

        find_by_order(k) --> returns iterator to the kth largest element counting from 0
        order_of_key(val) --> returns the number of items in a set that are strictly smaller than our item
*/

// typedef tree<
// int,
// null_type,
// less<int>,
// rb_tree_tag,
// tree_order_statistics_node_update>
// ordered_set;

#define MP make_pair
#define SORT(a) sort (a.begin(), a.end())
#define REVERSE(a) reverse (a.begin(), a.end())
#define ALL(a) a.begin(), a.end()
#define PI acos(-1)
#define ms(x,y) memset (x, y, sizeof (x))
#define inf 1e9
#define INF 1e16
#define pb push_back
#define MAX 100005
#define debug(a,b) cout<<a<<": "<<b<<endl
#define Debug cout<<"Reached here"<<endl
#define prnt(a) cout<<a<<"\n"
#define mod 1000000007LL
#define FOR(i,a,b) for (int i=(a); i<(b); i++)
#define FORr(i,a,b) for (int i=(a); i>=(b); i--)
#define itrALL(c,itr) for(__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++)
#define lc ((node)<<1)
#define rc ((node)<<1|1)
#define VecPrnt(v) FOR(J,0,v.size()) cout<<v[J]<<" "; cout<<endl
#define endl "\n"
#define PrintPair(x) cout<<x.first<<" "<<x.second<<endl
#define EPS 1e-9
#define ArrPrint(a,st,en) for(int J=st; J<=en; J++) cout<<a[J]<<" "; cout<<endl;

/* Direction Array */

// int fx[]={1,-1,0,0};
// int fy[]={0,0,1,-1};
// int fx[]={0,0,1,-1,-1,1,-1,1};
// int fy[]={-1,1,0,0,1,1,-1,-1};

template <class T> inline T bigmod(T p, T e, T M)
{
      ll ret = 1;
      for (; e > 0; e >>= 1)
      {
            if (e & 1) ret = (ret * p) % M;
            p = (p * p) % M;
      } return (T)ret;
}

template <class T> inline T gcd(T a, T b) {if (b == 0)return a; return gcd(b, a % b);}
template <class T> inline T modinverse(T a, T M) {return bigmod(a, M - 2, M);}
template <class T> inline T lcm(T a, T b) {a = abs(a); b = abs(b); return (a / gcd(a, b)) * b;}
template <class T, class X> inline bool getbit(T a, X i) { T t = 1; return ((a & (t << i)) > 0);}
template <class T, class X> inline T setbit(T a, X i) { T t = 1; return (a | (t << i)); }
template <class T, class X> inline T resetbit(T a, X i) { T t = 1; return (a & (~(t << i)));}

inline ll getnum()
{
      char c = getchar();
      ll num, sign = 1;
      for (; c < '0' || c > '9'; c = getchar())if (c == '-')sign = -1;
      for (num = 0; c >= '0' && c <= '9';)
      {
            c -= '0';
            num = num * 10 + c;
            c = getchar();
      }
      return num * sign;
}

inline ll power(ll a, ll b)
{
      ll multiply = 1;
      FOR(i, 0, b)
      {
            multiply *= a;
      }
      return multiply;
}

/***************** END OF HEADER *****************/

const int N = 1e5+5;
int wa[N],wb[N],wv[N],wc[N];
int r[N],sa[N],rak[N], height[N];

int cmp(int *r,int a,int b,int l)
{
      return r[a] == r[b] && r[a+l] == r[b+l];
}

void da(int *r,int *sa,int n,int m)
{
      int i,j,p,*x=wa,*y=wb,*t;
      for( i=0;i<m;i++) wc[i]=0;
      for( i=0;i<n;i++) wc[x[i]=r[i]] ++;
      for( i=1;i<m;i++) wc[i] += wc[i-1];
      for( i= n-1;i>=0;i--)sa[--wc[x[i]]] = i;
      for( j= 1,p=1;p<n;j*=2,m=p){
            for(p=0,i=n-j;i<n;i++)y[p++] = i;
            for(i=0;i<n;i++)if(sa[i] >= j) y[p++] = sa[i] - j;
            for(i=0;i<n;i++)wv[i] = x[y[i]];
            for(i=0;i<m;i++) wc[i] = 0;
            for(i=0;i<n;i++) wc[wv[i]] ++;
            for(i=1;i<m;i++) wc[i] += wc[i-1];
            for(i=n-1;i>=0;i--) sa[--wc[wv[i]]] = y[i];
            for(t=x,x=y,y=t,p=1,x[sa[0]] = 0,i=1;i<n;i++) x[sa[i]]= cmp(y,sa[i-1],sa[i],j) ? p-1:p++;

      }
}

void calheight(int *r,int *sa,int n)
{

      int i,j,k=0;
      for(i=1;i<=n;i++) rak[sa[i]] = i;
      for(i=0;i<n;height[rak[i++]] = k ) {

            for(k?k--:0, j=sa[rak[i]-1] ; r[i+k] == r[j+k] ; k ++) ;
      }

}

int dp[N][22];

void initRMQ(int n)
{
      for(int i= 1;i<=n;i++) dp[i][0] = height[i];
      for(int j= 1; (1<<j) <= n; j ++ ){
            for(int i = 1; i + (1<<j) - 1 <= n ; i ++ ) {
                  dp[i][j] = min(dp[i][j-1] , dp[i + (1<<(j-1))][j-1]);
            }
      }

}
int askRMQ(int L,int R)
{
      int k = 0;
      while((1<<(k+1)) <= R-L+1) k++;
      return min(dp[L][k], dp[R - (1<<k) + 1][k]);
}

int len, L[N], R[N];
char s[N];
vi occ[N];

void clear()
{
	FOR(i,0,len+1) occ[i].clear();
}

void simulate(stack<pii> &st, int idx, int *aux, bool flag)
{
	while(!st.empty() && st.top().first>=height[idx])
		st.pop();

	if(!st.empty()) aux[idx]=st.top().second;
	else
	{
		if(flag) aux[idx]=1;
		else aux[idx]=len+1;
	}

	st.push({height[idx],idx});
}

void calc(bool flag)
{
	stack<pii> st;
	if(flag)
	{
		FOR(i,2,len+1) simulate(st,i,L,1);
	}
	else
	{
		FORr(i,len,2) simulate(st,i,R,0);
	}
}

void buildSA()
{
	len=strlen(s);
	for(int i=0; i<len; i++)
		r[i]=s[i]-'a'+1;
	r[len]=0;

	da(r,sa,len+1,27);
	calheight(r,sa,len);

	// FOR(i,1,len+1) cout<<sa[i]<<" "<<s+sa[i]<<endl;
	// FOR(i,2,len+1) cout<<height[i]<<" "; cout<<endl;

	FOR(i,2,len+1) occ[height[i]].pb(i);

	calc(1); calc(0);

	// FOR(i,2,len+1) cout<<L[i]<<" "; cout<<endl;
	// FOR(i,2,len+1) cout<<R[i]<<" "; cout<<endl;
}

void solve(ll &t, ll &ans)
{
	FOR(i,1,len+1)
	{
		int ptr=0;

		FOR(j,0,occ[i].size())
		{
			int idx=occ[i][j], val=i;
			// cout<<"for height: "<<i<<" index: "<<idx<<endl;
			if(idx<ptr) continue;
			int lptr=L[idx], rptr=R[idx];
			int minv=inf;
			if(lptr!=1) minv=min(minv,i-height[lptr]);
			if(rptr!=len+1) minv=min(minv,i-height[rptr]);

			val=min(val,minv);

			ll x=rptr-lptr;
			ans+=val*x*x;

			t-=val*x;

			ptr=rptr;
		}
	}
}

int main()
{
      // ios_base::sync_with_stdio(0);
      // cin.tie(NULL); cout.tie(NULL);
      // freopen("in.txt","r",stdin);

      int test, cases = 1;

      scanf("%d", &test);

      while(test--)
      {
      	scanf("%s", s);

      	buildSA();
      	ll total=((ll)len*(len+1))/2, ans=0;
      	solve(total,ans);

      	ll out=total+ans;
      	printf("%lld\n", out);

      	clear();
      }

      return 0;
}


#include<bits/stdc++.h>

using namespace std;

const int LEN = 1e5 + 5, MAXS = 2 * LEN, K = 26;
int go[MAXS][K], slink[MAXS], length[MAXS], p, q, size = 2, last = 1, n;
long long dp[MAXS];
bool clones[MAXS], marks[MAXS];
char s[LEN], t[LEN];

void extend (int c)
{
	for (p = last; !go[p][c]; p = slink[p])
	    go[p][c] = size;

	q = go[p][c];
	length[size] = length[last] + 1;
	last = size++;

	if (!p)
	    slink[last] =  1;
	else if (length[p] + 1 == length[q])
		slink[last] = q;
	else
	{
		clones[size] = true;
		for (length[size] = length[p] + 1; go[p][c] == q; p = slink[p])
		    go[p][c] = size;

		memcpy(go[size], go[q], sizeof go[size]);
		slink[size] = slink[q];
		slink[last] = slink[q] = size++;
	}
}

void init(){
	size = 2;
	last = 1;
	memset(length, 0 , sizeof(length));
	memset(marks, 0 , sizeof(marks));
	memset(go, 0 , sizeof(go));
	memset(clones, 0 , sizeof(clones));
	memset(slink, 0 , sizeof(slink));
	memset(dp, 0 , sizeof(dp));
}

void build ()
{
	fill(go[0], go[0] + K, 1);
	length[0] = -1;

	for (int i = 0; s[i]; ++i)
	    extend(s[i] - 'a');

}

void build_matches( int st )
{
	for( int i = 0; i < K; i++ )
	{
		int to = go[st][i];

		if(to)
		{
			if( !marks[ to ] )
				{
					marks[to] = true;
					build_matches(to);
				}
		}
	}

	dp[ slink[st] ] += dp[st];
	return;
}

void build_matches( )
{
	for( int i = 2; i <= size; i++ )
		if( !clones[ i ])
			dp[ i ] = 1;
	marks[ 1 ] = true;
	build_matches( 1 );
}

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	cin >> n;
	for( int i = 0; i < n; i++ ){
		init();
		cin >> s;
		build();
		build_matches();
		long long res = 0;
		for(int i = size; i >= 2; i-- ){
			int l = length[i] - length[slink[i]];
			res += 1LL * dp[i] * dp[i] * (l);
		}

		cout << res - 1 << endl;
	}
}


#include<bits/stdc++.h>

using namespace std;

const int LEN = 1e5 + 5, MAXS = 2 * LEN, K = 26;
int go[MAXS][K], slink[MAXS], length[MAXS], p, q, size = 2, last = 1, n;
long long dp[MAXS];
bool clones[MAXS], marks[MAXS];
char s[LEN], t[LEN];

void extend (int c)
{
	for (p = last; !go[p][c]; p = slink[p])
	    go[p][c] = size;

	q = go[p][c];
	length[size] = length[last] + 1;
	last = size++;

	if (!p)
	    slink[last] =  1;
	else if (length[p] + 1 == length[q])
		slink[last] = q;
	else
	{
		clones[size] = true;
		for (length[size] = length[p] + 1; go[p][c] == q; p = slink[p])
		    go[p][c] = size;

		memcpy(go[size], go[q], sizeof go[size]);
		slink[size] = slink[q];
		slink[last] = slink[q] = size++;
	}
}

void init(){
	size = 2;
	last = 1;
	memset(length, 0 , sizeof(length));
	memset(marks, 0 , sizeof(marks));
	memset(go, 0 , sizeof(go));
	memset(clones, 0 , sizeof(clones));
	memset(slink, 0 , sizeof(slink));
	memset(dp, 0 , sizeof(dp));
}

void build ()
{
	fill(go[0], go[0] + K, 1);
	length[0] = -1;

	for (int i = 0; s[i]; ++i)
	    extend(s[i] - 'a');

}

void build_matches( int st )
{
	for( int i = 0; i < K; i++ )
	{
		int to = go[st][i];

		if(to)
		{
			if( !marks[ to ] )
				{
					marks[to] = true;
					build_matches(to);
				}
		}
	}

	dp[ slink[st] ] += dp[st];
	return;
}

void build_matches( )
{
	for( int i = 2; i <= size; i++ )
		if( !clones[ i ])
			dp[ i ] = 1;
	marks[ 1 ] = true;
	build_matches( 1 );
}

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	cin >> n;
	for( int i = 0; i < n; i++ ){
		init();
		cin >> s;
		build();
		build_matches();
		long long res = 0;
		for(int i = size - 1; i >= 2; i-- ){
			int l = length[i] - length[slink[i]];
			res += 1LL * dp[i] * dp[i] * (l);
		}

		cout << res << endl;
	}
}


#include<bits/stdc++.h>

using namespace std;

const int LEN = 1e5 + 5, MAXS = 2 * LEN, K = 26;
int go[MAXS][K], slink[MAXS], length[MAXS], p, q, size = 2, last = 1, n;
int dp[MAXS];
bool clones[MAXS];
char s[LEN], t[LEN];

void extend (int c)
{
	for (p = last; !go[p][c]; p = slink[p])
	    go[p][c] = size;

	q = go[p][c];
	length[size] = length[last] + 1;
	last = size++;

	if (!p)
	    slink[last] =  1;
	else if (length[p] + 1 == length[q])
		slink[last] = q;
	else
	{
		clones[size] = true;
		for (length[size] = length[p] + 1; go[p][c] == q; p = slink[p])
		    go[p][c] = size;

		memcpy(go[size], go[q], sizeof go[size]);
		slink[size] = slink[q];
		slink[last] = slink[q] = size++;
	}
}

void init(){
	size = 2;
	last = 1;
	memset(length, 0 , sizeof(length));
	memset(go, 0 , sizeof(go));
	memset(clones, 0 , sizeof(clones));
	memset(slink, 0 , sizeof(slink));
	memset(dp, 0 , sizeof(dp));
}

void build ()
{
	fill(go[0], go[0] + K, 1);
	length[0] = -1;

	for (int i = 0; s[i]; ++i)
	    extend(s[i] - 'a');

}

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	cin >> n;
	for( int i = 0; i < n; i++ ){
		init();
		cin >> s;
		build();

		for( int i = 2; i <= last; i++ ){
			if(!clones[i])
			{
				dp[i]++;
			}
		}

		for(int i = last; i >= 1; i-- ){
			int to = slink[i];
			dp[to] += dp[i];
		}
		long long res = 0;
		for(int i = size - 1; i >= 2; i-- ){
			int l = length[i] - length[slink[i]];
			res += 1LL * dp[i] * dp[i] * (l);
		}

		cout << res << endl;
	}
}


#include<bits/stdc++.h>

using namespace std;

const int LEN = 1e5 + 5, MAXS = 2 * LEN, K = 26;
int go[MAXS][K], slink[MAXS], length[MAXS], p, q, size = 2, last = 1, n;
long long dp[MAXS];
bool clones[MAXS];
char s[LEN], t[LEN];

void extend (int c)
{
	for (p = last; !go[p][c]; p = slink[p])
	    go[p][c] = size;

	q = go[p][c];
	length[size] = length[last] + 1;
	last = size++;

	if (!p)
	    slink[last] =  1;
	else if (length[p] + 1 == length[q])
		slink[last] = q;
	else
	{
		clones[size] = true;
		for (length[size] = length[p] + 1; go[p][c] == q; p = slink[p])
		    go[p][c] = size;

		memcpy(go[size], go[q], sizeof go[size]);
		slink[size] = slink[q];
		slink[last] = slink[q] = size++;
	}
}

void init(){
	size = 2;
	last = 1;
	memset(length, 0 , sizeof(length));
	memset(go, 0 , sizeof(go));
	memset(clones, 0 , sizeof(clones));
	memset(slink, 0 , sizeof(slink));
	memset(dp, 0 , sizeof(dp));
}

void build ()
{
	fill(go[0], go[0] + K, 1);
	length[0] = -1;

	for (int i = 0; s[i]; ++i)
	    extend(s[i] - 'a');

}

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	cin >> n;
	for( int i = 0; i < n; i++ ){
		init();
		cin >> s;
		build();

		for( int i = 2; i <= size - 1; i++ ){
			if(!clones[i])
				dp[i]++;
		}

		for(int i = size - 1; i >= 2; i-- ){
			int to = slink[i];
			dp[to] += dp[i];
		}

		long long res = 0;
		for(int i = size - 1; i >= 2; i-- ){
			int l = length[i] - length[slink[i]];
			res += 1LL * dp[i] * dp[i] * (l);
		}

		cout << res << endl;
	}
}


#include<bits/stdc++.h>

using namespace std;

const int LEN = 1e5 + 5, MAXS = 2 * LEN, K = 26;
int go[MAXS][K], slink[MAXS], length[MAXS], p, q, size = 2, last = 1;
long long dp[MAXS];
bool clones[MAXS], marks[MAXS];
char s[LEN], t[LEN];

void extend (int c)
{
	for (p = last; !go[p][c]; p = slink[p])
	    go[p][c] = size;

	q = go[p][c];
	length[size] = length[last] + 1;
	last = size++;

	if (!p)
	    slink[last] =  1;
	else if (length[p] + 1 == length[q])
		slink[last] = q;
	else
	{
		clones[size] = true;
		for (length[size] = length[p] + 1; go[p][c] == q; p = slink[p])
		    go[p][c] = size;

		memcpy(go[size], go[q], sizeof go[size]);
		slink[size] = slink[q];
		slink[last] = slink[q] = size++;
	}
}

void init(){
	p = q = 0;
	size = 2;
	last = 1;
	memset(length, 0 , sizeof(length));
	memset(marks, 0 , sizeof(marks));
	memset(go, 0 , sizeof(go));
	memset(clones, 0 , sizeof(clones));
	memset(slink, 0 , sizeof(slink));
	memset(dp, 0 , sizeof(dp));
}

void build ()
{
	fill(go[0], go[0] + K, 1);
	length[0] = -1;

	for (int i = 0; s[i]; ++i)
	    extend(s[i] - 'a');

}

void build_matches( int st )
{
	for( int i = 0; i < K; i++ )
	{
		int to = go[st][i];

		if(to)
		{
			if( !marks[ to ] )
				{
					marks[to] = true;
					build_matches(to);
				}
		}
	}

	dp[ slink[st] ] += dp[st];
	return;
}

void build_matches( )
{
	for( int i = 2; i <= size; i++ )
		if( !clones[ i ])
			dp[ i ] = 1;
	marks[ 1 ] = true;
	build_matches( 1 );
}

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	cin >> n;
	for( int i = 0; i < n; i++ ){
		init();
		cin >> s;
		build();
		build_matches();
		long long res = 0;
		for(int i = size; i >= 2; i-- ){
			int l = length[i] - length[slink[i]];
			res += 1LL * dp[i] * dp[i] * (l);
		}

		cout << res - 1 << endl;
	}
}


#include<bits/stdc++.h>

using namespace std;

const int LEN = 1e5 + 5, MAXS = 2 * LEN, K = 26;
int go[MAXS][K], slink[MAXS], length[MAXS], p, q, size = 2, last = 1, n;
int dp[MAXS];
bool clones[MAXS];
char s[LEN], t[LEN];

void extend (int c)
{
	for (p = last; !go[p][c]; p = slink[p])
	    go[p][c] = size;

	q = go[p][c];
	length[size] = length[last] + 1;
	last = size++;

	if (!p)
	    slink[last] =  1;
	else if (length[p] + 1 == length[q])
		slink[last] = q;
	else
	{
		clones[size] = true;
		for (length[size] = length[p] + 1; go[p][c] == q; p = slink[p])
		    go[p][c] = size;

		memcpy(go[size], go[q], sizeof go[size]);
		slink[size] = slink[q];
		slink[last] = slink[q] = size++;
	}
}

void init(){
	size = 2;
	last = 1;
	memset(length, 0 , sizeof(length));
	memset(go, 0 , sizeof(go));
	memset(clones, 0 , sizeof(clones));
	memset(slink, 0 , sizeof(slink));
	memset(dp, 0 , sizeof(dp));
}

void build ()
{
	fill(go[0], go[0] + K, 1);
	length[0] = -1;

	for (int i = 0; s[i]; ++i)
	    extend(s[i] - 'a');

}

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	cin >> n;
	for( int i = 0; i < n; i++ ){
		init();
		cin >> s;
		build();

		for( int i = 2; i <= size - 1; i++ ){
			if(!clones[i])
			{
				dp[i]++;
			}
		}

		for(int i = last; i >= 1; i-- ){
			int to = slink[i];
			dp[to] += dp[i];
		}
		long long res = 0;
		for(int i = size - 1; i >= 2; i-- ){
			int l = length[i] - length[slink[i]];
			res += 1LL * dp[i] * dp[i] * (l);
		}

		cout << res << endl;
	}
}


#include<bits/stdc++.h>

using namespace std;

const int LEN = 1e5 + 5, MAXS = 2 * LEN, K = 26;
int go[MAXS][K], slink[MAXS], length[MAXS], p, q, size = 2, last = 1;
long long dp[MAXS];
bool clones[MAXS], marks[MAXS];
char s[LEN], t[LEN];

void extend (int c)
{
	for (p = last; !go[p][c]; p = slink[p])
	    go[p][c] = size;

	q = go[p][c];
	length[size] = length[last] + 1;
	last = size++;
	dp[last] = 1;
	if (!p)
	    slink[last] =  1;
	else if (length[p] + 1 == length[q])
		slink[last] = q;
	else
	{
		clones[size] = true;
		for (length[size] = length[p] + 1; go[p][c] == q; p = slink[p])
		    go[p][c] = size;
		dp[size] = 0;
		memcpy(go[size], go[q], sizeof go[size]);
		slink[size] = slink[q];
		slink[last] = slink[q] = size++;
	}
}

void init(){
	p = q = 0;
	size = 2;
	last = 1;
	memset(length, 0 , sizeof(length));
	memset(marks, 0 , sizeof(marks));
	memset(go, 0 , sizeof(go));
	memset(clones, 0 , sizeof(clones));
	memset(slink, 0 , sizeof(slink));
	memset(dp, 0 , sizeof(dp));
}

void build ()
{
	fill(go[0], go[0] + K, 1);
	length[0] = -1;

	for (int i = 0; s[i]; ++i)
	    extend(s[i] - 'a');

}

long long build_matches( )
{
	vector<int> v, vis(size, 0);
	queue<int> q;
	q.push(1); vis[1] = 1;
	while (q.size()) {
		int u = q.front(); q.pop();
		v.push_back(u);
		for (int i = 0; i < K; i++) {
			if (go[u][i] > 1 && !vis[go[u][i]]) {
				q.push(go[u][i]);
				vis[go[u][i]] = 1;
			}
		}
	}
	long long res = 0;
	for (int i = v.size() - 1; i > 0; i--) {
		int u = v[i];
		dp[slink[u]] += dp[u];
		res += (long long) dp[u] * dp[u] * (length[u]- length[slink[u]]);
	}
	return res;
}

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int n;
	cin >> n;
	for( int i = 0; i < n; i++ ){
		init();
		cin >> s;
		build();
		cout << build_matches() << endl;
	}
}


#include<bits/stdc++.h>

using namespace std;

const int LEN = 1e5 + 5, MAXS = 2 * LEN, K = 26;
int go[MAXS][K], slink[MAXS], length[MAXS], p, q, size = 2, last = 1;
long long dp[MAXS];
bool clones[MAXS], marks[MAXS];
char s[LEN], t[LEN];

void extend (int c)
{
	for (p = last; !go[p][c]; p = slink[p])
	    go[p][c] = size;

	q = go[p][c];
	length[size] = length[last] + 1;
	last = size++;

	if (!p)
	    slink[last] =  1;
	else if (length[p] + 1 == length[q])
		slink[last] = q;
	else
	{
		clones[size] = true;
		for (length[size] = length[p] + 1; go[p][c] == q; p = slink[p])
		    go[p][c] = size;

		memcpy(go[size], go[q], sizeof go[size]);
		slink[size] = slink[q];
		slink[last] = slink[q] = size++;
	}
}

void init(){
	p = q = 0;
	size = 2;
	last = 1;
	memset(length, 0 , sizeof(length));
	memset(marks, 0 , sizeof(marks));
	memset(go, 0 , sizeof(go));
	memset(clones, 0 , sizeof(clones));
	memset(slink, 0 , sizeof(slink));
	memset(dp, 0 , sizeof(dp));
}

void build ()
{
	fill(go[0], go[0] + K, 1);
	length[0] = -1;

	for (int i = 0; s[i]; ++i)
	    extend(s[i] - 'a');

}

void build_matches( int st )
{
	for( int i = 0; i < K; i++ )
	{
		int to = go[st][i];

		if(to)
		{
			if( !marks[ to ] )
				{
					marks[to] = true;
					build_matches(to);
				}
		}
	}

	dp[ slink[st] ] += dp[st];
	return;
}

void build_matches( )
{
	for( int i = 2; i <= size; i++ )
		if( !clones[ i ])
			dp[ i ] = 1;
	marks[ 1 ] = true;
	build_matches( 1 );
}

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int n;
	cin >> n;
	for( int i = 0; i < n; i++ ){
		init();
		cin >> s;
		build();
		build_matches();
		long long res = 0;
		for(int i = size; i >= 2; i-- ){
			int l = length[i] - length[slink[i]];
			res += 1LL * dp[i] * dp[i] * (l);
		}

		cout << res - 1 << endl;
	}
}


#include<bits/stdc++.h>
#define long long long
using namespace std;
const int N=1e5;

struct suffixAutomata
{
  struct node
  {
    int len,link,next[26];
  }aut[N+N+2];//2*insrt function call
  int sz,last,occur[N+N+2];
  vector<int>tree[N+N+2];
  void clean(int n=N)//must call before insert
  {
    for(int i=0;i<=n+n;i++)
    {
      memset(aut[i].next,0,sizeof(aut[i].next));
      tree[i].clear();occur[i]=0;
    }
    sz=0,last=0,aut[0].link=-1;
  }
  void insrt(int c)
  {
    int now=++sz;aut[now].len=aut[last].len+1;
    int p,q,cl;occur[now]=1;
    for(p=last;p!=-1&&!aut[p].next[c];p=aut[p].link)
      aut[p].next[c]=now;
    if(p==-1)
      aut[now].link=0;
    else
    {
      q=aut[p].next[c];
      if(aut[p].len+1==aut[q].len)
        aut[now].link=q;
      else
      {
        cl=++sz;aut[cl].len=aut[p].len+1;
        aut[cl].link=aut[q].link;
        memcpy(aut[cl].next,aut[q].next,sizeof(aut[cl].next));
        for( ;p!=-1&&aut[p].next[c]==q;p=aut[p].link)
          aut[p].next[c]=cl;
        aut[now].link=aut[q].link=cl;
      }
    }
    last=now;
  }
  void dfs(int now)
  {
    for(auto x:tree[now])
    {
      dfs(x);occur[now]+=occur[x];
    }
  }
  void makeTree(void)
  {
    for(int i=1;i<=sz;i++)
      tree[aut[i].link].push_back(i);
    dfs(0);
  }
  long cal(void)
  {
    long ans=0;
    for(int i=1;i<=sz;i++)
    {
      long sub=aut[i].len-aut[aut[i].link].len;
      ans+=sub*occur[i]*occur[i];
    }
    return ans;
  }
}sam;
int main()
{
  ios_base::sync_with_stdio(0);cin.tie(0);
  int n;cin>>n;
  for(int i=1;i<=n;i++)
  {
    string s;cin>>s;
    sam.clean(s.size());
    for(auto x:s)sam.insrt(x-'a');
    sam.makeTree();cout<<sam.cal()<<"\n";
  }
  return 0;
}


#include<bits/stdc++.h>
#define long long long
using namespace std;
const int N=1e5;

struct suffixAutomata
{
  struct node
  {
    int len,link,next[26];
  }aut[N+N+2];//2*insrt function call
  int sz,last,occur[N+N+2];
  vector<int>tree[N+N+2];
  void clean(int n=N)//must call before insert
  {
    for(int i=0;i<=n+n;i++)
    {
      memset(aut[i].next,0,sizeof(aut[i].next));
      tree[i].clear();
    }
    sz=0,last=0,aut[0].link=-1;
  }
  void insrt(int c)
  {
    int now=++sz;aut[now].len=aut[last].len+1;
    int p,q,cl;occur[now]=1;
    for(p=last;p!=-1&&!aut[p].next[c];p=aut[p].link)
      aut[p].next[c]=now;
    if(p==-1)
      aut[now].link=0;
    else
    {
      q=aut[p].next[c];
      if(aut[p].len+1==aut[q].len)
        aut[now].link=q;
      else
      {
        cl=++sz;aut[cl].len=aut[p].len+1;
        aut[cl].link=aut[q].link;
        memcpy(aut[cl].next,aut[q].next,sizeof(aut[cl].next));
        for( ;p!=-1&&aut[p].next[c]==q;p=aut[p].link)
          aut[p].next[c]=cl;
        aut[now].link=aut[q].link=cl;
      }
    }
    last=now;
  }
  void dfs(int now)
  {
    for(auto x:tree[now])
    {
      dfs(x);occur[now]+=occur[x];
    }
  }
  void makeTree(void)
  {
    for(int i=1;i<=sz;i++)
      tree[aut[i].link].push_back(i);
    dfs(0);
  }
  long cal(void)
  {
    long ans=0;
    for(int i=1;i<=sz;i++)
    {
      long sub=aut[i].len-aut[aut[i].link].len;
      ans+=sub*occur[i]*occur[i];
    }
    return ans;
  }
}sam;
int main()
{
  ios_base::sync_with_stdio(0);cin.tie(0);
  int n;cin>>n;
  for(int i=1;i<=n;i++)
  {
    string s;cin>>s;
    sam.clean(s.size());
    for(auto x:s)sam.insrt(x-'a');
    sam.makeTree();cout<<sam.cal()<<"\n";
  }
  return 0;
}


#include<cstdio>
#include<cstring>
typedef long long ll;
const int N(1e5+5);
template<const int maxn>class Suffix_Automaton
{
	private:
		int fa[maxn],son[maxn][26],size[maxn],len[maxn],last,cnt,tmp[maxn],rnk[maxn];
		inline const void extend(const char &c)
		{
			int p(last);
			const int np(++cnt);
			len[last=np]=len[p]+(size[np]=1);
			for (;p&&!son[p][c];p=fa[p])son[p][c]=np;
			if (!p)return fa[np]=1,void();
			const int q(son[p][c]);
			if (len[q]==len[p]+1)return fa[np]=q,void();
			const int nq(++cnt);len[nq]=len[p]+1;
			memcpy(son[nq],son[q],sizeof(son[q]));
			fa[nq]=fa[q];fa[np]=fa[q]=nq;
			for (;son[p][c]==q;p=fa[p])son[p][c]=nq;
		}
		inline const void radixsort()
		{
			for (int i(1);i<=cnt;i++)tmp[len[i]]++;
			for (int i(1);i<=cnt;i++)tmp[i]+=tmp[i-1];
			for (int i(cnt);i;i--)rnk[tmp[len[i]]--]=i;
		}
	public:
		inline const ll build(char *s)
		{
			last=cnt=1;
			memset(size,0,sizeof(size));
			memset(tmp,0,sizeof(tmp));
			memset(son,0,sizeof(son));
			memset(fa,0,sizeof(fa));
			const int n(strlen(s+1));
			for (int i(1);i<=n;i++)extend(s[i]-'a');
			radixsort();
			for (int i(cnt);i;i--)size[fa[rnk[i]]]+=size[rnk[i]];
			ll ans(0ll);
			for (int i(1);i<=cnt;i++)ans+=1ll*(len[i]-len[fa[i]])*size[i]*size[i];
			return ans;
		}
};
Suffix_Automaton<N<<1>sam;
int n;
char s[N];
int main()
{
	int n;
	scanf("%d",&n);
	while (n--)scanf("%s",s+1),printf("%lld\n",sam.build(s));
	return 0;
}


#include<cstdio>
#include<cstring>
typedef long long ll;
const int N(1e5+5);
template<const int maxn>class Suffix_Automaton
{
	private:
		int fa[maxn],son[maxn][26],size[maxn],len[maxn],last,cnt,tmp[maxn],rnk[maxn];
		inline const void extend(const char &c)
		{
			int p(last);
			const int np(++cnt);
			len[last=np]=len[p]+(size[np]=1);
			for (;p&&!son[p][c];p=fa[p])son[p][c]=np;
			if (!p)return fa[np]=1,void();
			const int q(son[p][c]);
			if (len[q]==len[p]+1)return fa[np]=q,void();
			const int nq(++cnt);len[nq]=len[p]+1;
			memcpy(son[nq],son[q],sizeof(son[q]));
			fa[nq]=fa[q];fa[np]=fa[q]=nq;
			for (;son[p][c]==q;p=fa[p])son[p][c]=nq;
		}
		inline const void radixsort()
		{
			for (int i(1);i<=cnt;i++)tmp[len[i]]++;
			for (int i(1);i<=cnt;i++)tmp[i]+=tmp[i-1];
			for (int i(cnt);i>=1;i--)rnk[tmp[len[i]]--]=i;
		}
	public:
		inline const ll build(char *s)
		{
			last=cnt=1;
			memset(tmp,0,sizeof(tmp));
			memset(son,0,sizeof(son));
			memset(fa,0,sizeof(fa));
			const int n(strlen(s+1));
			for (int i(1);i<=n;i++)extend(s[i]-'a');
			radixsort();
			for (int i(cnt);i;i--)size[fa[rnk[i]]]+=size[rnk[i]];
			ll ans(0ll);
			for (int i(1);i<=cnt;i++)ans+=1ll*(len[i]-len[fa[i]])*size[i]*size[i];
			return ans;
		}
};
Suffix_Automaton<N<<1>sam;
int n;
char s[N];
int main()
{
	int n;
	scanf("%d",&n);
	while (n--)scanf("%s",s+1),printf("%lld\n",sam.build(s));
	return 0;
}


#include<bits/stdc++.h>

using namespace std;
#define ll long long
#define ull unsigned long long
#define mp make_pair
typedef pair<int, int> pii;
typedef priority_queue<int, vector<int>, greater<int> > small_heap;
typedef priority_queue<int> big_heap;
const int N = 1e5 + 100;

struct graph {
    int head[N<<1], nxt[N << 1], to[N << 1], sz;

    void init() { memset(head, 0, sizeof(head)), sz = 0; }

    graph() { init(); }

    void push(int a, int b) { nxt[++sz] = head[a], to[sz] = b, head[a] = sz; }

    int &operator[](const int a) { return to[a]; }
} g;

char str[N];
ll answer;

struct Suffix_Automata {
    //数据要开两倍
    int maxlen[N << 1],  trans[N << 1][26], link[N << 1], Size, Last;
    int endcnt[N << 1];  //该状态表示的字符集在原串中出现的次数

    void init() {
        Size = Last = 1;
    }  //需要手动初始化
    void clean(int whi_node) {
        memset(trans[whi_node], 0, sizeof(trans[whi_node]));
        link[whi_node] = 0;
    }

    inline void Extend(int id) {
        int cur = (++Size), p;
        clean(cur);
        maxlen[cur] = maxlen[Last] + 1;
        endcnt[cur] = 1;
        for (p = Last; p && !trans[p][id]; p = link[p]) trans[p][id] = cur;
        if (!p)
            link[cur] = 1;
        else {
            int q = trans[p][id];
            if (maxlen[q] == maxlen[p] + 1)
                link[cur] = q;
            else {
                int clone = (++Size);
                endcnt[clone] = 0;
                maxlen[clone] = maxlen[p] + 1;
                memcpy(trans[clone], trans[q], sizeof(trans[q]));
                link[clone] = link[q];
                for (; p && trans[p][id] == q; p = link[p]) trans[p][id] = clone;
                link[cur] = link[q] = clone;
            }
        }
        Last = cur;
    }

    void dfs(int u) {
        for (int i = g.head[u]; i; i = g.nxt[i]) {
            dfs(g[i]);
            endcnt[u] += endcnt[g[i]];
        }
        if (u != 1)answer += 1ll * endcnt[u] * endcnt[u] * (maxlen[u] - maxlen[link[u]]);
        maxlen[u]  = 0;
    }

    void suf_slink_tree() {  //建立后缀树
        g.init();
        for (int i = 2; i <= Size; i++) {
            g.push(link[i], i);
        }
        dfs(1);
        printf("%lld\n", answer);
    }
} T;

int main() {
    int t;
    scanf("%d", &t);
    while (t--) {
        T.init();
        g.init();
        T.clean(1);
        answer = 0;
        scanf("%s", str + 1);
        int len = strlen(str + 1);
        for (int i = 1; i <= len; i++)T.Extend(str[i] - 'a');
        T.suf_slink_tree();
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i, j, k) for (int i = int(j); i <= int(k); ++ i)
#define dwn(i, j, k) for (int i = int(j); i >= int(k); -- i)
typedef long long ll;
const int N = 1e5 + 7;
struct SAM {
struct node {
	int son[26], pre, len;
}a[N << 1];
int f[N << 1];
vector<int> e[N << 1];
int tot, tail = 0;
void init() {
	rep(i, 0, tot) {
		e[i].clear(), f[i] = 0;
		memset(a[i].son, 0, sizeof(a[i].son));
		a[i].pre = a[i].len = 0;
	}
	tot = tail = 0;
}
void ins(int k) {
	int p, q, np = ++ tot, nq;
	a[np].len = a[tail].len + 1; f[np] = 1;
	for (p = tail; p && !a[p].son[k]; p = a[p].pre) a[p].son[k] = np;
	if (!a[p].son[k]) { // 如果p为0
		a[p].son[k] = np;
		a[np].pre = p;
	}
	else {
		q = a[p].son[k];
		if (a[q].len == a[p].len + 1) a[np].pre = q;
		else {
			a[nq = ++ tot] = a[q];
			a[nq].len = a[p].len + 1;
			a[q].pre = a[np].pre = nq;
			for (; a[p].son[k] == q; p = a[p].pre) a[p].son[k] = nq;
		}
	}
	tail = np;
}
void dfs(int u) {
	for (int v: e[u]) dfs(v);
	f[a[u].pre] += f[u];
}
void solve() {
	rep(i, 1, tot) e[a[i].pre].push_back(i);
	dfs(0);
	ll ans = 0;
	rep(i, 1, tot) {
		ans += 1ll * (a[i].len - a[a[i].pre].len) * f[i] * f[i];
	}
	printf("%lld\n", ans);
}
}sam;
char s[N];
int main() {
	int _;
	scanf("%d", &_);
	while (_ --) {
		sam.init();
		scanf("%s", s);
		int len = strlen(s);
		rep(i, 0, len - 1) sam.ins(s[i] - 'a');
		sam.solve();
	}
}


#include <bits/stdc++.h>
using namespace std;
#define PB push_back
#define ZERO (1e-10)
#define INF int(1e9+1)
#define CL(A,I) (memset(A,I,sizeof(A)))
#define DEB printf("DEB!\n");
#define D(X) cout<<"  "<<#X": "<<X<<endl;
#define EQ(A,B) (A+ZERO>B&&A-ZERO<B)
typedef long long ll;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef pair<int,int> ii;
typedef vector<ii> vii;
#define IN(n) int n;scanf("%d",&n);
#define FOR(i, m, n) for (int i(m); i < n; i++)
#define F(n) FOR(i,0,n)
#define FF(n) FOR(j,0,n)
#define FT(m, n) FOR(k, m, n)
#define aa first
#define bb second
void ga(int N,int *A){F(N)scanf("%d",A+i);}
#define MX (1<<19)
typedef unsigned char uc;
uc M[]={128,64,32,16,8,4,2,1};
#define SI (sizeof(int))
#define tg(i) ((t[(i)>>3]&M[(i)&7])?1:0)
#define ts(i, b) t[(i)>>3]=(b) ? (M[(i)&7]|t[(i)>>3]):((~M[(i)&7])&t[(i)>>3])
#define chr(i) (cs==SI?((int*)s)[i]:((uc*)s)[i])
#define lms(i) (i>0&&tg(i)&&!tg(i-1))
void buc(uc *s,int *b,int n,int K,int cs,bool e){
    int S(0);
    F(K+1)b[i]=0;
    F(n)++b[chr(i)];
    F(K+1)S+=b[i],b[i]=e?S:S-b[i];
}
void isa(uc*t,int*SA,uc*s,int*b,int n,int K,int cs,bool e){
    int j;
    buc(s,b,n,K,cs,e);
    F(n)if((j=SA[i]-1)>=0&&!tg(j))
        SA[b[chr(j)]++]=j;
}
void sas(uc*t,int*SA,uc*s,int*b,int n,int K,int cs,bool e){
    int j;
    buc(s,b,n,K,cs,e);
    for(int i(n-1);~i;--i)
        if((j=SA[i]-1)>=0&&tg(j))
            SA[--b[chr(j)]]=j;
}
void sa(uc*s,int*SA,int n,int K=256,int cs=1){
    static int b[MX];
    int j,n1(0),h(0),w(-1),p,f;
    uc *t=(uc*)malloc(n>>3|1);
    ts(n-2,0),ts(n-1,1);
    for(int i(n-3);~i;--i)
        ts(i,(chr(i)<chr(i+1)||(chr(i)==chr(i+1)&&tg(i+1)==1))?1:0);
    buc(s,b,n,K,cs,1);
    F(n)SA[i]=-1;
    FT(1,n)if(lms(k))SA[--b[chr(k)]]=k;
    isa(t,SA,s,b,n,K,cs,0);
    sas(t,SA,s,b,n,K,cs,1);
    F(n)if(lms(SA[i]))SA[n1++]=SA[i];
    FT(n1,n)SA[k]=-1;
    F(n1){
        p=SA[i],f=0;
        F(n)if(!~w||chr(p+i)!=chr(w+i)||tg(p+i)!=tg(w+i))f=1,i=n;
            else if(i>0&&(lms(p+i)||lms(w+i)))break;
        if(f)++h,w=p;
        p>>=1,SA[n1+p]=h-1;
    }
    for(int i(n-1),j(n-1);i>=n1;--i)
        if(SA[i]>=0)SA[j--]=SA[i];
    int *SA1=SA,*s1=SA+n-n1;
    if(h<n1)sa((uc*)s1,SA1,n1,h-1,SI);
    else F(n1)SA1[s1[i]]=i;
    buc(s,b,n,K,cs,1);
    j=0;FT(1,n)if(lms(k))s1[j++]=k;
    F(n1)SA1[i]=s1[SA1[i]];
    FT(n1,n)SA[k]=-1;
    for(int i(n1-1);~i;--i)
        j=SA[i],SA[i]=-1,SA[--b[chr(j)]]=j;
    isa(t,SA,s,b,n,K,cs,0),sas(t,SA,s,b,n,K,cs,1);
    free(t);
}
void lcp(int*sa,int*lcp,int n,uc*s){
    static int r[MX];
    F(n)r[sa[i]]=i,lcp[i]=0;;
    int h(0);F(n)if(r[i]<n-1){
        for(int j(sa[r[i]+1]);s[i+h]==s[j+h];++h);
        lcp[r[i]]=h;
        if(h)--h;
    }
}
#define PW(A) ((A)*ll(A))
int L,C[MX],SA[MX];
uc s[MX];
vii v;
ll S,l,t,T;
int main(void){
    IN(_)F(_){
        scanf("%s",s),L=strlen((char*)s)+1,sa(s,SA,L),lcp(SA,C,L,s),S=0,v.clear();
        F(L){
            if(!v.empty())assert(v.back().aa<=L-1-SA[i]);//TODO ASS
            v.PB({L-1-SA[i],1});
            t=0,l=L-1-SA[i];
            while(!v.empty()&&v.back().aa>=C[i])
                S+=PW(t)*(l-v.back().aa),t+=v.back().bb,l=v.back().aa,v.pop_back();
            S+=PW(t)*(l-C[i]),v.PB({C[i],t});
        }
        printf("%lld\n",S);
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define PB push_back
#define ZERO (1e-10)
#define INF int(1e9+1)
#define CL(A,I) (memset(A,I,sizeof(A)))
#define DEB printf("DEB!\n");
#define D(X) cout<<"  "<<#X": "<<X<<endl;
#define EQ(A,B) (A+ZERO>B&&A-ZERO<B)
typedef long long ll;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef pair<int,int> ii;
typedef vector<ii> vii;
#define IN(n) int n;scanf("%d",&n);
#define FOR(i, m, n) for (int i(m); i < n; i++)
#define F(n) FOR(i,0,n)
#define FF(n) FOR(j,0,n)
#define FT(m, n) FOR(k, m, n)
#define aa first
#define bb second
void ga(int N,int *A){F(N)scanf("%d",A+i);}
#define MX (1<<19)
typedef unsigned char uc;
uc M[]={128,64,32,16,8,4,2,1};
#define SI (sizeof(int))
#define tg(i) ((t[(i)>>3]&M[(i)&7])?1:0)
#define ts(i, b) t[(i)>>3]=(b) ? (M[(i)&7]|t[(i)>>3]):((~M[(i)&7])&t[(i)>>3])
#define chr(i) (cs==SI?((int*)s)[i]:((uc*)s)[i])
#define lms(i) (i>0&&tg(i)&&!tg(i-1))
void buc(uc *s,int *b,int n,int K,int cs,bool e){
    int S(0);
    F(K+1)b[i]=0;
    F(n)++b[chr(i)];
    F(K+1)S+=b[i],b[i]=e?S:S-b[i];
}
void isa(uc*t,int*SA,uc*s,int*b,int n,int K,int cs,bool e){
    int j;
    buc(s,b,n,K,cs,e);
    F(n)if((j=SA[i]-1)>=0&&!tg(j))
        SA[b[chr(j)]++]=j;
}
void sas(uc*t,int*SA,uc*s,int*b,int n,int K,int cs,bool e){
    int j;
    buc(s,b,n,K,cs,e);
    for(int i(n-1);~i;--i)
        if((j=SA[i]-1)>=0&&tg(j))
            SA[--b[chr(j)]]=j;
}
void sa(uc*s,int*SA,int n,int K=256,int cs=1){
    static int b[MX];
    int j,n1(0),h(0),w(-1),p,f;
    uc *t=(uc*)malloc(n>>3|1);
    ts(n-2,0),ts(n-1,1);
    for(int i(n-3);~i;--i)
        ts(i,(chr(i)<chr(i+1)||(chr(i)==chr(i+1)&&tg(i+1)==1))?1:0);
    buc(s,b,n,K,cs,1);
    F(n)SA[i]=-1;
    FT(1,n)if(lms(k))SA[--b[chr(k)]]=k;
    isa(t,SA,s,b,n,K,cs,0);
    sas(t,SA,s,b,n,K,cs,1);
    F(n)if(lms(SA[i]))SA[n1++]=SA[i];
    FT(n1,n)SA[k]=-1;
    F(n1){
        p=SA[i],f=0;
        F(n)if(!~w||chr(p+i)!=chr(w+i)||tg(p+i)!=tg(w+i))f=1,i=n;
            else if(i>0&&(lms(p+i)||lms(w+i)))break;
        if(f)++h,w=p;
        p>>=1,SA[n1+p]=h-1;
    }
    for(int i(n-1),j(n-1);i>=n1;--i)
        if(SA[i]>=0)SA[j--]=SA[i];
    int *SA1=SA,*s1=SA+n-n1;
    if(h<n1)sa((uc*)s1,SA1,n1,h-1,SI);
    else F(n1)SA1[s1[i]]=i;
    buc(s,b,n,K,cs,1);
    j=0;FT(1,n)if(lms(k))s1[j++]=k;
    F(n1)SA1[i]=s1[SA1[i]];
    FT(n1,n)SA[k]=-1;
    for(int i(n1-1);~i;--i)
        j=SA[i],SA[i]=-1,SA[--b[chr(j)]]=j;
    isa(t,SA,s,b,n,K,cs,0),sas(t,SA,s,b,n,K,cs,1);
    free(t);
}
void lcp(int*sa,int*lcp,int n,uc*s){
    static int r[MX];
    F(n)r[sa[i]]=i,lcp[i]=0;;
    int h(0);F(n)if(r[i]<n-1){
        for(int j(sa[r[i]+1]);s[i+h]==s[j+h];++h);
        lcp[r[i]]=h;
        if(h)--h;
    }
}
#define PW(A) ((A)*ll(A))
int L,C[MX],SA[MX];
uc s[MX];
vii v;
ll S,l,t,T;
int main(void){
    IN(_)F(_){
        scanf("%s",s),L=strlen((char*)s)+1,sa(s,SA,L),lcp(SA,C,L,s),S=0,v.clear();
        F(L){
            v.PB({L-1-SA[i],1});
            t=0,l=L-1-SA[i];
            while(!v.empty()&&v.back().aa>=C[i])
                S+=PW(t)*(l-v.back().aa),t+=v.back().bb,l=v.back().aa,v.pop_back();
            v.PB({C[i],t});
        }
        printf("%lld\n",S);
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define ll long long
#define all(aaa) aaa.begin(), aaa.end()

const int N = 1e5 + 5, L = 20;
int suf[L][N], cl[L][N], ct[N], lcp[N], pos[N], lb[N], rb[N];
string s;
int n;

void build() {
    memset(ct, 0, sizeof(ct));

    for (int i = 0; i < n; i++) {
        cl[0][i] = s[i];
        ct[s[i]]++;
        suf[0][i] = i;
    }

    for (int i = 1; i < N; i++) {
        ct[i] += ct[i - 1];
    }

    for (int i = n - 1; i >= 0; i--) {
        suf[0][--ct[s[i]]] = i;
    }

    for (int i = 1; i < L; i++) {
        memset(ct, 0, sizeof(ct));

        for (int j = 0; j < n; j++) {
            suf[i - 1][j] = (suf[i - 1][j] - (1 << (i - 1)) % n + n) % n;
            ct[cl[i - 1][j]]++;
        }

        for (int j = 1; j < N; j++) {
            ct[j] += ct[j - 1];
        }

        for (int j = n - 1; j >= 0; j--) {
            suf[i][--ct[cl[i - 1][suf[i - 1][j]]]] = suf[i - 1][j];
        }

        for (int j = 0, k = 0; j < n; j++) {
            cl[i][suf[i][j]] = k;
            if (j < n - 1 && cl[i - 1][suf[i][j]] != cl[i - 1][suf[i][j + 1]] || cl[i - 1][(suf[i][j] + (1 << (i - 1))) % n] !=
                    cl[i - 1][(suf[i][j + 1] + (1 << (i - 1))) % n]) {
                k++;
            }
        }
    }

    for (int i = 0; i < n; i++) {
        pos[suf[L - 1][i]] = i;
    }

    for (int i = 0, k = 0; i < n; i++) {
        int j = pos[i];
        if (j == n - 1) {
            k = 0;
            continue;
        }
        j = suf[L - 1][j + 1];
        while (i + k < n && j + k < n && s[i + k] == s[j + k])
            k++;
        lcp[pos[i]] = k;
        k = max(0, k - 1);
    }
}

signed main() {
#ifdef HOME
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int t;
    cin >> t;

    while (t--) {
        cin >> s;
        s += "#";
        n = s.size();
        build();

        vector<int> v;
        for (int i = 0; i < n; i++) {
            while (!v.empty() && lcp[v.back()] >= lcp[i])
                v.pop_back();
            lb[i] = v.empty() ? -1 : v.back();
            v.push_back(i);
        }
        v.clear();
        for (int i = n - 1; i >= 0; i--) {
            while (!v.empty() && lcp[v.back()] > lcp[i])
                v.pop_back();
            rb[i] = v.empty() ? n : v.back();
            v.push_back(i);
        }

        ll ans = 0;

        for (int i = 0; i < n; i++) {
            int anime = 0;
            if (lb[i] != -1)
                anime = max(anime, lcp[lb[i]]);
            if (rb[i] != n)
                anime = max(anime, lcp[rb[i]]);
            ll len = rb[i] - lb[i];
            ans += len * len * (lcp[i] - anime);

            if (i > 0)
                anime = max(anime, lcp[i - 1]);
            anime = max(anime, lcp[i]);

            ans += n - suf[L - 1][i] - 1 - anime;
        }

        cout << ans << "\n";
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int N = 1234567;

char s[N];

struct suffix_automata {
  struct state {
    int len;
    int link;
    int cnt = 0;
    map<char, int> next;
  } st[N];

  int last, sz;

  suffix_automata() {
    last = 0;
    sz = 1;
    st[last].link = -1;
    st[last].len = 0;
  }

  void add(char c) {
    int cur = sz++;
    st[cur].cnt = 1;
    st[cur].len = st[last].len + 1;
    int p;
    for (p = last; p > -1 && !st[p].next.count(c); p = st[p].link) {
      st[p].next[c] = cur;
    }
    if (p == -1) {
      st[cur].link = 0;
    } else {
      int q = st[p].next[c];
      if (st[p].len + 1 == st[q].len) {
        st[cur].link = q;
      } else {
        int clone = sz++;
        st[clone].len = st[p].len + 1;
        st[clone].link = st[q].link;
        st[clone].next = st[q].next;
        for (; p > -1 && st[p].next[c] == q; p = st[p].link) {
          st[p].next[c] = clone;
        }
        st[cur].link = st[q].link = clone;
      }
    }
    last = cur;
  }

  long long solve() {
    vector<int> p(sz);
    for (int i = 0; i < sz; i++) p[i] = i;
    sort(p.begin(), p.end(), [&](const int i, const int j) {
      return st[i].len > st[j].len;
    });
    for (int i = 0; i < sz; i++) {
      int link = st[p[i]].link;
      st[link].cnt += st[p[i]].cnt;
    }
    long long ret = 0;
    for (int i = 1; i < sz; i++) {
      int link = st[i].link;
      ret += (long long) (st[i].len - st[link].len) * st[i].cnt * (st[i].cnt + 1) / 2;
    }
    return ret;
  }

} automata;

int main() {
  scanf("%s", s);
  for (int i = 0; s[i]; i++) {
    automata.add(s[i]);
  }
  int ans = automata.solve();
  printf("%d\n", ans);
  return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int MAX=1e5+9,BASE=1999,MOD=1e8+7;
string s;
int a[MAX],p[MAX],n,k,pw[MAX],h[MAX];
long long ans,sum;
inline int m(int x){return x<0?x+MOD:x;}
int hsh(int l,int r){return m(l<=r?(h[r]-(l?h[l-1]:0))*1LL*pw[n-l-1]%MOD:-1);}
bool cmp(int i,int j)
{
	int l=0,r=n-max(i,j)+1;
	while (l<r-1) (hsh(i,(l+r>>1)+i-1)==hsh(j,(l+r>>1)+j-1)?l:r)=(l+r>>1);
	k=l;
	return (max(i,j)+l==n?i>j:s[i+l]<s[j+l]);
}
vector<pair<int,int> > l,r;
int main()
{
	ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	cin>>s,n=s.size();
	for (int i=0;i<n;i++) p[i]=i,pw[i]=(i?1LL*pw[i-1]*BASE%MOD:1),h[i]=((i?h[i-1]:0)+1LL*s[i]*pw[i])%MOD;
	sort(p,p+n,cmp);
	for (int i=0;i<n-1;i++) cmp(p[i],p[i+1]),a[i]=k;
	for (int i=0;i<n-1;i++)
	{
		int t=1;
		while (l.size() && l.back().first>=a[i]) sum-=1LL*l.back().second*l.back().first,t+=l.back().second,l.pop_back();
		sum+=1LL*a[i]*t;
		ans+=sum;
		l.push_back({a[i],t});
	}
	cout<<ans+1LL*n*(n+1)/2;
}


#include<bits/stdc++.h>
using namespace std;
const int MAX=1e5+9,BASE=17,MOD=1e8+9;
string s;
int a[MAX],p[MAX],n,k,pw[MAX],h[MAX];
long long ans,sum;
inline int m(int x){return x<0?x+MOD:x;}
int hsh(int l,int r){return m(l<=r?(h[r]-(l?h[l-1]:0))*1LL*pw[n-l-1]%MOD:-1);}
bool cmp(int i,int j)
{
	int l=0,r=n-max(i,j)+1;
	while (l<r-1) (hsh(i,(l+r>>1)+i-1)==hsh(j,(l+r>>1)+j-1)?l:r)=(l+r>>1);
	k=l;
	return (max(i,j)+l==n?i>j:s[i+l]<s[j+l]);
}
vector<pair<int,int> > l,r;
int main()
{
	ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	cin>>s,n=s.size();
	for (int i=0;i<n;i++) p[i]=i,pw[i]=(i?1LL*pw[i-1]*BASE%MOD:1),h[i]=((i?h[i-1]:0)+1LL*s[i]*pw[i])%MOD;
	sort(p,p+n,cmp);
	for (int i=0;i<n-1;i++) cmp(p[i],p[i+1]),a[i]=k;
	for (int i=0;i<n-1;i++)
	{
		int t=1;
		while (l.size() && l.back().first>=a[i]) sum-=1LL*l.back().second*l.back().first,t+=l.back().second,l.pop_back();
		sum+=1LL*a[i]*t;
		ans+=sum;
		l.push_back({a[i],t});
	}
	cout<<ans+1LL*n*(n+1)/2;
}


#include<bits/stdc++.h>
using namespace std;
const int MAX=1e5+9,BASE=1999,MOD=1e9+7;
string s;
int a[MAX],p[MAX],n,k,pw[MAX],h[MAX];
long long ans,sum;
int hsh(int l,int r){return ((l<=r?1LL*(h[r]-(l?h[l-1]:0))*pw[n-l-1]%MOD:-1)+MOD)%MOD;}
bool cmp(int i,int j)
{
	int l=0,r=n-max(i,j)+1,mid;
	while (l<r-1)
	{
		mid=l+r>>1;
		if (hsh(i,mid+i-1)==hsh(j,mid+j-1)) l=mid; else r=mid;
	}
	k=l;
	return (max(i,j)+l==n?i>j:s[i+l]<s[j+l]);
}
vector<pair<int,int> > l,r;
int main()
{
	cin>>s,n=s.size();
	for (int i=0;i<n;i++) p[i]=i,pw[i]=(i?1LL*pw[i-1]*BASE%MOD:1),h[i]=((i?h[i-1]:0)+1LL*s[i]*pw[i])%MOD;
	sort(p,p+n,cmp);
	for (int i=0;i<n-1;i++) cmp(p[i],p[i+1]),a[i]=k;
	for (int i=0;i<n-1;i++)
	{
		int t=1;
		while (l.size() && l.back().first>=a[i]) sum-=l.back().second*l.back().first,t+=l.back().second,l.pop_back();
		sum+=1LL*a[i]*t;
		ans+=sum;
		l.push_back({a[i],t});
	}
	cout<<ans+1LL*n*(n+1)/2;
}


#include<bits/stdc++.h>
using namespace std;
const int MAX=1e5+9,BASE=97,MOD=1e8+9;
string s;
int a[MAX],n,k,gap,sa[MAX],pos[MAX],tmp[MAX],lcp[MAX];
long long ans,sum;
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()
{
	for (int i=0;i<n;i++) sa[i]=i,pos[i]=s[i];
	for (gap=1;;gap*=2)
	{
		sort(sa,sa+n,sufCmp);
		for (int i=0;i<n-1;i++) tmp[i+1]=tmp[i]+sufCmp(sa[i],sa[i+1]);
		for (int i=0;i<n;i++)   pos[sa[i]]=tmp[i];
		if (tmp[n-1]==n-1) break;
	}
}
void buildLCP()
{
	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++;
			a[pos[i]]=k;
			if (k) k--;
		}
}
vector<pair<int,int> > l;
int main()
{
	ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	cin>>s,n=s.size();
	buildSA(),buildLCP();
	for (int i=0;i<n-1;i++)
	{
		int t=1;
		while (l.size() && l.back().first>=a[i]) sum-=1LL*l.back().second*l.back().first,t+=l.back().second,l.pop_back();
		sum+=1LL*a[i]*t;
		ans+=sum;
		l.push_back({a[i],t});
	}
	cout<<ans+1LL*n*(n+1)/2;
}


#include<bits/stdc++.h>
using namespace std;
const int MAX=1e5+9,BASE=97,MOD=1e8+9;
string s;
int a[MAX],p[MAX],n,k,pw[MAX],h[MAX];
long long ans,sum;
inline int m(int x){return x<0?x+MOD:x;}
int hsh(int l,int r){return m(l<=r?(h[r]-(l?h[l-1]:0))*1LL*pw[n-l-1]%MOD:-1);}
bool cmp(int i,int j)
{
	int l=0,r=n-max(i,j)+1;
	while (l<r-1) (hsh(i,(l+r>>1)+i-1)==hsh(j,(l+r>>1)+j-1)?l:r)=(l+r>>1);
	k=l;
	return (max(i,j)+l==n?i>j:s[i+l]<s[j+l]);
}
vector<pair<int,int> > l,r;
int main()
{
	ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	cin>>s,n=s.size();
	for (int i=0;i<n;i++) p[i]=i,pw[i]=(i?1LL*pw[i-1]*BASE%MOD:1),h[i]=((i?h[i-1]:0)+1LL*s[i]*pw[i])%MOD;
	sort(p,p+n,cmp);
	for (int i=0;i<n-1;i++) cmp(p[i],p[i+1]),a[i]=k;
	for (int i=0;i<n-1;i++)
	{
		int t=1;
		while (l.size() && l.back().first>=a[i]) sum-=1LL*l.back().second*l.back().first,t+=l.back().second,l.pop_back();
		sum+=1LL*a[i]*t;
		ans+=sum;
		l.push_back({a[i],t});
	}
	cout<<ans+1LL*n*(n+1)/2;
}


#include<bits/stdc++.h>
using namespace std;
const int MAX=1e5+9,BASE=1999,MOD=1e8+9;
string s;
int a[MAX],p[MAX],n,k,pw[MAX],h[MAX];
long long ans,sum;
inline int m(int x){return x<0?x+MOD:x;}
int hsh(int l,int r){return m(l<=r?(h[r]-(l?h[l-1]:0))*1LL*pw[n-l-1]%MOD:-1);}
bool cmp(int i,int j)
{
	int l=0,r=n-max(i,j)+1;
	while (l<r-1) (hsh(i,(l+r>>1)+i-1)==hsh(j,(l+r>>1)+j-1)?l:r)=(l+r>>1);
	k=l;
	return (max(i,j)+l==n?i>j:s[i+l]<s[j+l]);
}
vector<pair<int,int> > l,r;
int main()
{
	ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	cin>>s,n=s.size();
	for (int i=0;i<n;i++) p[i]=i,pw[i]=(i?1LL*pw[i-1]*BASE%MOD:1),h[i]=((i?h[i-1]:0)+1LL*s[i]*pw[i])%MOD;
	sort(p,p+n,cmp);
	for (int i=0;i<n-1;i++) cmp(p[i],p[i+1]),a[i]=k;
	for (int i=0;i<n-1;i++)
	{
		int t=1;
		while (l.size() && l.back().first>=a[i]) sum-=1LL*l.back().second*l.back().first,t+=l.back().second,l.pop_back();
		sum+=1LL*a[i]*t;
		ans+=sum;
		l.push_back({a[i],t});
	}
	cout<<ans+1LL*n*(n+1)/2;
}


#include<bits/stdc++.h>
using namespace std;
const int MAX=1e5+9,BASE=1999,MOD=1e9+7;
string s;
int a[MAX],p[MAX],n,k,pw[MAX],h[MAX];
long long ans,sum;
inline int m(int x){return x<0?x+MOD:x;}
int hsh(int l,int r){return m(l<=r?(h[r]-(l?h[l-1]:0))*1LL*pw[n-l-1]%MOD:-1);}
bool cmp(int i,int j)
{
	int l=0,r=n-max(i,j)+1;
	while (l<r-1) (hsh(i,(l+r>>1)+i-1)==hsh(j,(l+r>>1)+j-1)?l:r)=(l+r>>1);
	k=l;
	return (max(i,j)+l==n?i>j:s[i+l]<s[j+l]);
}
vector<pair<int,int> > l,r;
int main()
{
	ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	cin>>s,n=s.size();
	for (int i=0;i<n;i++) p[i]=i,pw[i]=(i?1LL*pw[i-1]*BASE%MOD:1),h[i]=((i?h[i-1]:0)+1LL*s[i]*pw[i])%MOD;
	sort(p,p+n,cmp);
	for (int i=0;i<n-1;i++) cmp(p[i],p[i+1]),a[i]=k;
	for (int i=0;i<n-1;i++)
	{
		int t=1;
		while (l.size() && l.back().first>=a[i]) sum-=1LL*l.back().second*l.back().first,t+=l.back().second,l.pop_back();
		sum+=1LL*a[i]*t;
		ans+=sum;
		l.push_back({a[i],t});
	}
	cout<<ans+1LL*n*(n+1)/2;
}


#include<bits/stdc++.h>
using namespace std;
const int MAX=1e5+9,BASE=23,MOD=1e8+9;
string s;
int a[MAX],p[MAX],n,k,pw[MAX],h[MAX];
long long ans,sum;
inline int m(int x){return x<0?x+MOD:x;}
int hsh(int l,int r){return m(l<=r?(h[r]-(l?h[l-1]:0))*1LL*pw[n-l-1]%MOD:-1);}
bool cmp(int i,int j)
{
	int l=0,r=n-max(i,j)+1;
	while (l<r-1) (hsh(i,(l+r>>1)+i-1)==hsh(j,(l+r>>1)+j-1)?l:r)=(l+r>>1);
	k=l;
	return (max(i,j)+l==n?i>j:s[i+l]<s[j+l]);
}
vector<pair<int,int> > l,r;
int main()
{
	ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	cin>>s,n=s.size();
	for (int i=0;i<n;i++) p[i]=i,pw[i]=(i?1LL*pw[i-1]*BASE%MOD:1),h[i]=((i?h[i-1]:0)+1LL*s[i]*pw[i])%MOD;
	sort(p,p+n,cmp);
	for (int i=0;i<n-1;i++) cmp(p[i],p[i+1]),a[i]=k;
	for (int i=0;i<n-1;i++)
	{
		int t=1;
		while (l.size() && l.back().first>=a[i]) sum-=1LL*l.back().second*l.back().first,t+=l.back().second,l.pop_back();
		sum+=1LL*a[i]*t;
		ans+=sum;
		l.push_back({a[i],t});
	}
	cout<<ans+1LL*n*(n+1)/2;
}


#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;

const int maxn = 100005;
const int inf = 0x3f3f3f3f;

namespace suffix_array {
    int n, k;
    int rank[maxn], tmp[maxn];
    int level[maxn];

    bool compare_sa(int i, int j) {
	if (rank[i] != rank[j]) return rank[i] < rank[j];
	else {
	    int ri = i + k <= n ? rank[i + k] : -1;
	    int rj = j + k <= n ? rank[j + k] : -1;
	    return ri < rj;
	}
    }

    void construct_sa(string s, int* sa) {
	n = s.length();
	for (int i = 0; i <= n; i++) {
	    sa[i] = i;
	    rank[i] = i < n ? s[i] : -1;
	}
	for (k = 1; k <= n; k *= 2) {
	    sort(sa, sa + n + 1, compare_sa);
	    tmp[sa[0]] = 0;
	    for (int i = 1; i <= n; i++) {
		tmp[sa[i]] = tmp[sa[i-1]] + (compare_sa(sa[i-1], sa[i]) ? 1 : 0);
	    }
	    for (int i = 0; i <= n; i++) {
		rank[i] = tmp[i];
	    }
	}
    }

    void construct_lcp(string s, int* sa, int* lcp) {
	for (int i = 0; i <= n; i++) level[sa[i]] = i;
	int h = 0;
	lcp[0] = 0;
	for (int i = 0; i < n; i++) {
	    int j = sa[rank[i] - 1];
	    if (h > 0) h--;
	    for (; j + h < n && i + h < n; h++) {
		if (s[j + h] != s[i + h]) break;
	    }
	    lcp[rank[i] - 1] = h;
	}
    }
}

int sa[maxn], lcp[maxn];
int hight[maxn];
int dp[maxn][30];

void rmq_init(int n) {
    for (int i = 1 ; i <= n ; i++) dp[i][0] = hight[i];

    for (int j = 1 ; (1<<j) <= n ; j++)
        for ( int i = 1 ; i + (1<<j) - 1 <= n ; i++)
            dp[i][j] = min(dp[i][j-1],dp[i+(1<<(j-1))][j-1]);
}

int rmq(int l,int r) {
    if (l>r) swap(l,r);
    int k = 0 ;
    while (1<<(k+1)<=r-l+1) k++;
    int res = min(dp[l][k],dp[r-(1<<k)+1][k]);
    return res;

}

void solve(string s) {
    int n = s.length();
    long long ans = n * (n + 1) / 2;
    suffix_array::construct_sa(s, sa);
    suffix_array::construct_lcp(s, sa, lcp);
    for (int i = 0; i < n; i++) hight[i + 1] = lcp[i];
    rmq_init(n);

    //for (int i = 0; i <= n; i++) {
//	cout << i << ":" << " " << lcp[i] <<endl;
    // }
    for (int i = 1; i <= n; i++) {
	int l = 0, r = i;
	while (r > l + 1) {
	    int mid = (l + r) / 2;
	    if (rmq(mid, i - 1) >= hight[i]) r = mid;
	    else l = mid;
	}
	int x = i, y = n + 1;
	while (y > x + 1) {
	    int mid = (x + y) / 2;
	    if (rmq(i + 1, mid) > hight[i]) x = mid;
	    else y = mid;
	}
	ans += 1LL * hight[i] * (x - i + 1) * (i - r + 1);
    }
    cout << ans << endl;
}

int main() {
    ios::sync_with_stdio(false);
    string s;
    cin >> s;
    solve(s);
    return 0;
}


#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;

const int maxn = 100005;
const int inf = 0x3f3f3f3f;

namespace suffix_array {
    int n, k;
    int rank[maxn], tmp[maxn];
    int level[maxn];

    bool compare_sa(int i, int j) {
	if (rank[i] != rank[j]) return rank[i] < rank[j];
	else {
	    int ri = i + k <= n ? rank[i + k] : -1;
	    int rj = j + k <= n ? rank[j + k] : -1;
	    return ri < rj;
	}
    }

    void construct_sa(string s, int* sa) {
	n = s.length();
	for (int i = 0; i <= n; i++) {
	    sa[i] = i;
	    rank[i] = i < n ? s[i] : -1;
	}
	for (k = 1; k <= n; k *= 2) {
	    sort(sa, sa + n + 1, compare_sa);
	    tmp[sa[0]] = 0;
	    for (int i = 1; i <= n; i++) {
		tmp[sa[i]] = tmp[sa[i-1]] + (compare_sa(sa[i-1], sa[i]) ? 1 : 0);
	    }
	    for (int i = 0; i <= n; i++) {
		rank[i] = tmp[i];
	    }
	}
    }

    void construct_lcp(string s, int* sa, int* lcp) {
	for (int i = 0; i <= n; i++) level[sa[i]] = i;
	int h = 0;
	lcp[0] = 0;
	for (int i = 0; i < n; i++) {
	    int j = sa[rank[i] - 1];
	    if (h > 0) h--;
	    for (; j + h < n && i + h < n; h++) {
		if (s[j + h] != s[i + h]) break;
	    }
	    lcp[rank[i] - 1] = h;
	}
    }
}

int sa[maxn], lcp[maxn];

int dp[maxn][30];
void rmq_init(int n) {
    for (int i = 1 ; i <= n ; i++) dp[i][0] = lcp[i];

    for (int j = 1 ; (1<<j) <= n ; j++)
        for ( int i = 1 ; i + (1<<j) - 1 <= n ; i++)
            dp[i][j] = min(dp[i][j-1],dp[i+(1<<(j-1))][j-1]);
}

int rmq(int l,int r) {
    if (l>r) swap(l,r);
    int k = 0 ;
    while (1<<(k+1)<=r-l+1) k++;
    int res = min(dp[l][k],dp[r-(1<<k)+1][k]);
    return res;

}

void solve(string s) {
    int n = s.length();
    long long ans = n * (n + 1) / 2;
    suffix_array::construct_sa(s, sa);
    suffix_array::construct_lcp(s, sa, lcp);
    rmq_init(n);

    for (int i = 0; i <= n; i++) {
	int l = 0, r = i;
	while (r > l + 1) {
	    int mid = (l + r) / 2;
	    if (rmq(mid, i - 1) >= lcp[i]) r = mid;
	    else l = mid;
	}
	int L = i, R = n + 1;
	while (R > L + 1) {
	    int mid = (L + R) / 2;
	    if (rmq(i + 1, mid) > lcp[i]) L = mid;
	    else R = mid;
	}
	ans += lcp[i] * (L - i + 1) * (i - r + 1);
    }
    cout << ans << endl;
}

int main() {
    ios::sync_with_stdio(false);
    string s;
    cin >> s;
    solve(s);
    return 0;
}


#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;

const int maxn = 100005;
const int inf = 0x3f3f3f3f;

namespace suffix_array {
    int n, k;
    int rank[maxn], tmp[maxn];
    int level[maxn];

    bool compare_sa(int i, int j) {
	if (rank[i] != rank[j]) return rank[i] < rank[j];
	else {
	    int ri = i + k <= n ? rank[i + k] : -1;
	    int rj = j + k <= n ? rank[j + k] : -1;
	    return ri < rj;
	}
    }

    void construct_sa(char* s, int* sa) {
	n = strlen(s);
	for (int i = 0; i <= n; i++) {
	    sa[i] = i;
	    rank[i] = i < n ? s[i] : -1;
	}
	for (k = 1; k <= n; k *= 2) {
	    sort(sa, sa + n + 1, compare_sa);
	    tmp[sa[0]] = 0;
	    for (int i = 1; i <= n; i++) {
		tmp[sa[i]] = tmp[sa[i-1]] + (compare_sa(sa[i-1], sa[i]) ? 1 : 0);
	    }
	    for (int i = 0; i <= n; i++) {
		rank[i] = tmp[i];
	    }
	}
    }

    void construct_lcp(char* s, int* sa, int* lcp) {
	for (int i = 0; i <= n; i++) level[sa[i]] = i;
	int h = 0;
	lcp[0] = 0;
	for (int i = 0; i < n; i++) {
	    int j = sa[rank[i] - 1];
	    if (h > 0) h--;
	    for (; j + h < n && i + h < n; h++) {
		if (s[j + h] != s[i + h]) break;
	    }
	    lcp[rank[i] - 1] = h;
	}
    }
}

int sa[maxn], lcp[maxn];
int hight[maxn];
int dp[maxn][30];
char str[maxn];

void rmq_init(int n) {
    for (int i = 1 ; i <= n ; i++) dp[i][0] = hight[i];

    for (int j = 1 ; (1<<j) <= n ; j++)
        for ( int i = 1 ; i + (1<<j) - 1 <= n ; i++)
            dp[i][j] = min(dp[i][j-1],dp[i+(1<<(j-1))][j-1]);
}

int rmq(int l,int r) {
    if (l>r) swap(l,r);
    int k = 0 ;
    while (1<<(k+1)<=r-l+1) k++;
    int res = min(dp[l][k],dp[r-(1<<k)+1][k]);
    return res;
}

void solve(char* s) {
    int n = strlen(s);
    long long ans = n * (n + 1) / 2;
    suffix_array::construct_sa(s, sa);
    suffix_array::construct_lcp(s, sa, lcp);
    for (int i = 0; i < n; i++) hight[i + 1] = lcp[i];
    rmq_init(n);

    //for (int i = 0; i <= n; i++) {
//	cout << i << ":" << " " << lcp[i] <<endl;
    // }
    for (int i = 1; i <= n; i++) {
	int l = 0, r = i;
	while (r > l + 1) {
	    int mid = (l + r) / 2;
	    if (rmq(mid, i - 1) >= hight[i]) r = mid;
	    else l = mid;
	}
	int x = i, y = n + 1;
	while (y > x + 1) {
	    int mid = (x + y) / 2;
	    if (rmq(i + 1, mid) > hight[i]) x = mid;
	    else y = mid;
	}
	ans += 1LL * hight[i] * (x - i + 1) * (i - r + 1);
    }
    cout << ans << endl;
}

int main() {
    ios::sync_with_stdio(false);
    cin >> str;
    solve(str);
    return 0;
}


#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;

const int maxn = 100005;
const int inf = 0x3f3f3f3f;

namespace suffix_array {
    int n, k;
    int rank[maxn], tmp[maxn];
    int level[maxn];

    bool compare_sa(int i, int j) {
	if (rank[i] != rank[j]) return rank[i] < rank[j];
	else {
	    int ri = i + k <= n ? rank[i + k] : -1;
	    int rj = j + k <= n ? rank[j + k] : -1;
	    return ri < rj;
	}
    }

    void construct_sa(string s, int* sa) {
	n = s.length();
	for (int i = 0; i <= n; i++) {
	    sa[i] = i;
	    rank[i] = i < n ? s[i] : -1;
	}
	for (k = 1; k <= n; k *= 2) {
	    sort(sa, sa + n + 1, compare_sa);
	    tmp[sa[0]] = 0;
	    for (int i = 1; i <= n; i++) {
		tmp[sa[i]] = tmp[sa[i-1]] + (compare_sa(sa[i-1], sa[i]) ? 1 : 0);
	    }
	    for (int i = 0; i <= n; i++) {
		rank[i] = tmp[i];
	    }
	}
    }

    void construct_lcp(string s, int* sa, int* lcp) {
	for (int i = 0; i <= n; i++) level[sa[i]] = i;
	int h = 0;
	lcp[0] = 0;
	for (int i = 0; i < n; i++) {
	    int j = sa[rank[i] - 1];
	    if (h > 0) h--;
	    for (; j + h < n && i + h < n; h++) {
		if (s[j + h] != s[i + h]) break;
	    }
	    lcp[rank[i] - 1] = h;
	}
    }
}

int sa[maxn], lcp[maxn];

int dp[maxn][30];
void rmq_init(int n) {
    for (int i = 1 ; i <= n ; i++) dp[i][0] = lcp[i - 1];

    for (int j = 1 ; (1<<j) <= n ; j++)
        for ( int i = 1 ; i + (1<<j) - 1 <= n ; i++)
            dp[i][j] = min(dp[i][j-1],dp[i+(1<<(j-1))][j-1]);
}

int rmq(int l,int r) {
    if (l>r) swap(l,r);
    int k = 0 ;
    while (1<<(k+1)<=r-l+1) k++;
    int res = min(dp[l][k],dp[r-(1<<k)+1][k]);
    return res;

}

void solve(string s) {
    int n = s.length();
    long long ans = n * (n + 1) / 2;
    suffix_array::construct_sa(s, sa);
    suffix_array::construct_lcp(s, sa, lcp);
    rmq_init(n);
    lcp[n] = 0;
    //for (int i = 0; i <= n; i++) {
//	cout << i << ":" << " " << lcp[i] <<endl;
    // }
    for (int i = 1; i <= n; i++) {
	int l = 0, r = i;
	while (r > l + 1) {
	    int mid = (l + r) / 2;
	    if (rmq(mid, i - 1) >= lcp[i - 1]) r = mid;
	    else l = mid;
	}
	int x = i, y = n + 1;
	while (y > x + 1) {
	    int mid = (x + y) / 2;
	    if (rmq(i + 1, mid) > lcp[i - 1]) x = mid;
	    else y = mid;
	}
	ans += 1LL * lcp[i - 1] * (x - i + 1) * (i - r + 1);
    }
    cout << ans << endl;
}

int main() {
    ios::sync_with_stdio(false);
    string s;
    cin >> s;
    solve(s);
    return 0;
}


#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;

const int maxn = 100005;
const int inf = 0x3f3f3f3f;

namespace suffix_array {
    int n, k;
    int rank[maxn], tmp[maxn];
    int level[maxn];

    bool compare_sa(int i, int j) {
	if (rank[i] != rank[j]) return rank[i] < rank[j];
	else {
	    int ri = i + k <= n ? rank[i + k] : -1;
	    int rj = j + k <= n ? rank[j + k] : -1;
	    return ri < rj;
	}
    }

    void construct_sa(string s, int* sa) {
	n = s.length();
	for (int i = 0; i <= n; i++) {
	    sa[i] = i;
	    rank[i] = i < n ? s[i] : -1;
	}
	for (k = 1; k <= n; k *= 2) {
	    sort(sa, sa + n + 1, compare_sa);
	    tmp[sa[0]] = 0;
	    for (int i = 1; i <= n; i++) {
		tmp[sa[i]] = tmp[sa[i-1]] + (compare_sa(sa[i-1], sa[i]) ? 1 : 0);
	    }
	    for (int i = 0; i <= n; i++) {
		rank[i] = tmp[i];
	    }
	}
    }

    void construct_lcp(string s, int* sa, int* lcp) {
	for (int i = 0; i <= n; i++) level[sa[i]] = i;
	int h = 0;
	lcp[0] = 0;
	for (int i = 0; i < n; i++) {
	    int j = sa[rank[i] - 1];
	    if (h > 0) h--;
	    for (; j + h < n && i + h < n; h++) {
		if (s[j + h] != s[i + h]) break;
	    }
	    lcp[rank[i] - 1] = h;
	}
    }
}

int sa[maxn], lcp[maxn];

int dp[maxn][30];
void rmq_init(int n) {
    for (int i = 1 ; i <= n ; i++) dp[i][0] = lcp[i];

    for (int j = 1 ; (1<<j) <= n ; j++)
        for ( int i = 1 ; i + (1<<j) - 1 <= n ; i++)
            dp[i][j] = min(dp[i][j-1],dp[i+(1<<(j-1))][j-1]);
}

int rmq(int l,int r) {
    if (l>r) swap(l,r);
    int k = 0 ;
    while (1<<(k+1)<=r-l+1) k++;
    int res = min(dp[l][k],dp[r-(1<<k)+1][k]);
    return res;

}

void solve(string s) {
    int n = s.length();
    long long ans = n * (n + 1) / 2;
    suffix_array::construct_sa(s, sa);
    suffix_array::construct_lcp(s, sa, lcp);
    rmq_init(n);
    lcp[n] = 0;

    for (int i = 0; i < n; i++) {
	int l = 0, r = i;
	while (r > l + 1) {
	    int mid = (l + r) / 2;
	    if (rmq(mid, i - 1) >= lcp[i]) r = mid;
	    else l = mid;
	}
	int x = i, y = n;
	while (y > x + 1) {
	    int mid = (x + y) / 2;
	    if (rmq(i + 1, mid) > lcp[i]) x = mid;
	    else y = mid;
	}
	ans += 1LL * lcp[i] * (x - i + 1) * (i - r + 1);
    }
    cout << ans << endl;
}

int main() {
    ios::sync_with_stdio(false);
    string s;
    cin >> s;
    solve(s);
    return 0;
}


#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <stack>
#define fst first
#define sec second
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define ms(a,x) memset(a,x,sizeof(a))
typedef long long LL;
#define pi pair < int ,int >
#define MP make_pair
using namespace std;
const double eps = 1E-8;
const int dx4[4]={1,0,0,-1};
const int dy4[4]={0,-1,1,0};
const int inf = 0x3f3f3f3f;
const int N=1E5+7;
int n,sa[N],rk[N],t[N],t2[N],cnt[N];
int height[N];
char s[N];
int cmp( int *r , int a,int b,int l){return r[a]==r[b]&&r[a+l]==r[b+l];}
void getSa( int n,int m)
{
    int *x = t;
    int *y = t2;
    ms(cnt,0);
    for ( int i = 0 ; i  < n ; i++) cnt[x[i]=s[i]]++;
    for ( int i = 1 ; i < m ; i++) cnt[i] +=cnt[i-1];
    for ( int i = n-1 ; i >= 0 ; i--) sa[--cnt[x[i]]] = i ;
    for ( int k = 1 ; k <= n ; k <<=1)
    {
        int p = 0 ;
        for ( int i = n-k ; i < n;  i++) y[p++] = i;
        for  ( int i = 0 ; i < n ; i++) if (sa[i]>=k) y[p++] = sa[i]-k;
        ms(cnt,0);
        for ( int i = 0 ; i <n ; i++) cnt[x[y[i]]]++;
        for ( int i = 0 ; i < m ; i++) cnt[i] +=cnt[i-1];
        for ( int i = n-1 ; i >= 0 ; i--) sa[--cnt[x[y[i]]]] = y[i];
        swap(x,y);
        p = 1;
        x[sa[0]] = 0 ;
        for ( int i = 1 ;i  <n ; i++)
            x[sa[i]] = cmp(y,sa[i-1],sa[i],k)?p-1:p++;
        if (p>=n) break;
        m = p;
    }
}
void getHeight( int n)
{
    int k = 0 ;
    for ( int i = 0 ; i < n ; i++) rk[sa[i]] = i ;
    height[0] =  0 ;
    for ( int  i = 0 ; i < n ; i++)
    {
        if (rk[i]==0) continue;
        if (k) k--;
        int j = sa[rk[i]-1];
        while (s[i+k]==s[j+k]) k++;
        height[rk[i]] = k;
    }
}
int getSuffix( char s[])
{
    int len =  strlen(s);
    int up = 0 ;
    for ( int i = 0 ; i < len ; i++)
    {
        int  val = s[i];
        up = max(up,val);
    }
    s[len++]='$';
    getSa(len,up+1);
    getHeight(len);
    return len;
}
LL cal( LL x)
{
    return x*(x+1)/2LL;
}
LL solve2(int n)
{
         for ( int i = 0 ; i < n ; i++) cout<<i<<" "<<height[i]<<endl;
    ms(cnt,0);
    int up = 0 ;
    for ( int i = 1 ; i < n;  i++) cnt[height[i]]++,up=max(up,height[i]);
    for ( int i = up-1 ; i >= 0 ; i--) cnt[i] = cnt[i]+cnt[i+1];
    LL res = 0LL ;
    for ( int i = 0 ; i <= up ; i++)
        res = res + cal(1LL*cnt[i]);
    return res;
}
/*
stack<int>stk;
int l[N],r[N];
bool visl[N],visr[N];

LL solve ( LL n)  //单调栈
{
    LL res = n*(n-1LL)/2LL;
    int up = 0 ;abaabaabaaba
    for ( int i = 1 ; i < n; i++) up = max(height[i],up);
 //   for ( int i = 1 ; i <= n ; i++) cout<<"i:"<<i<<" height[i]:"<<height[i]<<endl;
    height[0] = -1;
    height[n+1] = -1 ; //单调队列的哨兵
    stk.push(0);
    int x;
    for ( int i = 1 ; i <= n; i++)
    {
        for (x=stk.top();height[x]>=height[i];x=stk.top()) stk.pop();
        l[i] = x+1;
        stk.push(i);
//        cout<<"i:"<<i<<endl;
    }
    while (!stk.empty()) stk.pop() ; stk.push(n+1);
    for ( int i  = n ; i >=1 ; i--)
    {
        for (x=stk.top() ; height[x]>=height[i] ; x = stk.top()) stk.pop();
        r[i] = x-1;
        stk.push(i);
    }
  //  for ( int i = 1 ; i <= n ; i++)
//        cout<<"i:"<<i<<" l[i]:"<<l[i]<<" r[i]:"<<r[i]<<" height[i]:"<<height[i]<<" "<<(r[i]-l[i]+1)*height[i]<<endl;
//    int mxh = 0 ;
    ms(visl,false);
    ms(visr,false);
    int mxh = 0 ;
    int more =1;
    for ( int i = 1 ;i  <= n ; i++)
    {
        if (height[i]==0)
        {
            mxh = 0 ;
            continue;
        }
        if (visr[r[i]]&&visl[l[i]])
        {
            mxh = 0 ;
            continue;
        }
            if (i>1&&height[i-1]<height[i]) more = height[i]-height[i-1];
            if (i<n&&height[i+1]<height[i]) more = min(more,height[i]-height[i+1]);
            res = res + cal((r[i]-l[i]+1))*(more);
            mxh = max(mxh,height[i]);
            visr[r[i]] = true;
            visl[l[i]] = true;
//            cout<<"i:"<<i<<" res:"<<res<<endl;

      }

    return res;
}  */

int dp[N][30]; // for rmq;
void rmq_init( int n)
{
    for ( int i = 1 ; i <= n ; i++) dp[i][0] = height[i];

    for ( int j = 1 ; (1<<j) <= n ; j++)
        for ( int i = 1 ; i + (1<<j) - 1 <= n ; i++)
            dp[i][j] = min(dp[i][j-1],dp[i+(1<<(j-1))][j-1]);
}
int rmq( int l,int r)
{
    if (l>r) swap(l,r);
    int k = 0 ;
    while (1<<(k+1)<=r-l+1) k++;
    int res = min(dp[l][k],dp[r-(1<<k)+1][k]);
    return res;

}

LL solve( LL len)
{
    rmq_init(len);
    LL ans = len*(len+1)/2;

    //for ( int i = 1 ; i <= len ; i++) cout<<i<<" :"<<height[i]<<endl;
    for ( int i = 1 ; i <= len ; i++)
    {
        int l = 0 ,r = i ;
        while (r>l+1)
        {
            int mid = (l+r)/2;
            if (rmq(mid,i-1)>=height[i]) r = mid;
            else l = mid;
        }
        int L = i,R= len+1;
        while (R>L+1)
        {
            int mid = (L+R) / 2;
            if (rmq(i+1,mid)>height[i]) L = mid;
            else R = mid;
        }
//        cout<<"i:"<<height[i]<<"L:"<<L<<" r:"<<r<<" ans:"<<ans<<endl;
        ans +=1LL*height[i]*(L-i+1)*(i-r+1);
    }
    return ans;
}
int main()
{

        scanf("%s",s);
        int len = getSuffix(s);
        //cout<<"len:"<<len-1<<endl;
        LL ans = solve(len-1);
        cout << ans << endl;

    return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stack>
#define pa pair<int,int>
#define x first
#define y second
#define ll long long
#define mp make_pair
using namespace std;
const int N=1e5+10;
char s[N];
int n,rak[N<<1],rak1[N<<1],t[N],tmp[N],cnt[N],sa[N],h[N],v[N];
int top,L[N],R[N],tot;
ll sum,ans;
stack<pa> S;
int main(){
	//freopen("cf123d.in","r",stdin);
	scanf("%s",s+1);
	n=strlen(s+1);
	for(int i=1;i<=n;i++) t[s[i]]++;
	for(int i=1;i<=1e3;i++) t[i]+=t[i-1];
	for(int i=1;i<=n;i++) rak[i]=t[s[i]];

	for(int p=1,k=0;k!=n;p<<=1){
		memset(cnt,0,sizeof cnt);
		for(int i=1;i<=n;i++) cnt[rak[i+p]]++;
		for(int i=1;i<=n;i++) cnt[i]+=cnt[i-1];
		for(int i=n;i>=1;i--) tmp[cnt[rak[i+p]]--]=i;

		memset(cnt,0,sizeof cnt);
		for(int i=1;i<=n;i++) cnt[rak[i]]++;
		for(int i=1;i<=n;i++) cnt[i]+=cnt[i-1];
		for(int i=n;i>=1;i--) sa[cnt[rak[tmp[i]]]--]=tmp[i];
		memcpy(rak1,rak,sizeof(rak));
		k=1;rak[sa[1]]=1;
		for(int i=2;i<=n;i++){
			if(rak1[sa[i]]!=rak1[sa[i-1]] || rak1[sa[i]]==rak1[sa[i-1]] && rak1[sa[i]+p]!=rak1[sa[i-1]+p] )k++;
			rak[sa[i]]=k;
		}
	}
	for(int i=1,k=0;i<=n;i++){
		if(rak[i]==n){
			h[rak[i]]=0;continue;
		}
		if(k) k--;
		while(s[i+k]==s[sa[rak[i]+1]+k]) k++;
		h[rak[i]]=k;
	}
	ans=n*(n+1)>>1;sum=0;
	for(int i=1;i<=n;i++){
		pa temp=mp(h[i],1);
		while(!S.empty() && S.top().x>h[i]){
			sum-=(ll)S.top().x*S.top().y;
			temp.y+=S.top().y;
			S.pop();
		}
		sum+=(ll)temp.x*temp.y;
		ans+=sum;
		S.push(temp);
	}
	printf("%I64d\n",ans);
	return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stack>
#define pa pair<int,int>
#define x first
#define y second
#define ll long long
#define mp make_pair
using namespace std;
const int N=1e5+10;
char s[N];
int n,rak[N<<1],rak1[N<<1],t[N],tmp[N],cnt[N],sa[N],h[N],v[N];
ll sum,ans;
stack<pa> S;
int main(){
	//freopen("cf123d.in","r",stdin);
	scanf("%s",s+1);
	n=strlen(s+1);
	for(int i=1;i<=n;i++) t[s[i]]++;
	for(int i=1;i<=1e3;i++) t[i]+=t[i-1];
	for(int i=1;i<=n;i++) rak[i]=t[s[i]];

	for(int p=1,k=0;k!=n;p<<=1){
		memset(cnt,0,sizeof cnt);
		for(int i=1;i<=n;i++) cnt[rak[i+p]]++;
		for(int i=1;i<=n;i++) cnt[i]+=cnt[i-1];
		for(int i=n;i>=1;i--) tmp[cnt[rak[i+p]]--]=i;

		memset(cnt,0,sizeof cnt);
		for(int i=1;i<=n;i++) cnt[rak[i]]++;
		for(int i=1;i<=n;i++) cnt[i]+=cnt[i-1];
		for(int i=n;i>=1;i--) sa[cnt[rak[tmp[i]]]--]=tmp[i];
		memcpy(rak1,rak,sizeof(rak));
		k=1;rak[sa[1]]=1;
		for(int i=2;i<=n;i++){
			if(rak1[sa[i]]!=rak1[sa[i-1]] || rak1[sa[i]]==rak1[sa[i-1]] && rak1[sa[i]+p]!=rak1[sa[i-1]+p] )k++;
			rak[sa[i]]=k;
		}
	}
	for(int i=1,k=0;i<=n;i++){
		if(rak[i]==n){
			h[rak[i]]=0;continue;
		}
		if(k) k--;
		while(s[i+k]==s[sa[rak[i]+1]+k]) k++;
		h[rak[i]]=k;
	}
	ans=(ll)n*(ll)(n+1)>>1;sum=0;
	for(int i=1;i<=n;i++){
		pa temp=mp(h[i],1);
		while(!S.empty() && S.top().x>h[i]){
			sum-=(ll)S.top().x*S.top().y;
			temp.y+=S.top().y;
			S.pop();
		}
		sum+=1ll*temp.x*1ll*temp.y;
		ans+=sum;
		S.push(temp);
	}
	printf("%I64d\n",ans);
	return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stack>
#define pa pair<int,int>
#define x first
#define y second
#define ll long long
#define mp make_pair
using namespace std;
const int N=1e5+10;
char s[N];
int n,rak[N<<1],rak1[N<<1],t[N],tmp[N],cnt[N],sa[N],h[N],v[N];
int top,L[N],R[N],tot;
ll sum,ans;
stack<pa> S;
int main(){
	//freopen("cf123d.in","r",stdin);
	scanf("%s",s+1);
	n=strlen(s+1);
	for(int i=1;i<=n;i++) t[s[i]]++;
	for(int i=1;i<=1e3;i++) t[i]+=t[i-1];
	for(int i=1;i<=n;i++) rak[i]=t[s[i]];

	for(int p=1,k=0;k!=n;p<<=1){
		memset(cnt,0,sizeof cnt);
		for(int i=1;i<=n;i++) cnt[rak[i+p]]++;
		for(int i=1;i<=n;i++) cnt[i]+=cnt[i-1];
		for(int i=n;i>=1;i--) tmp[cnt[rak[i+p]]--]=i;

		memset(cnt,0,sizeof cnt);
		for(int i=1;i<=n;i++) cnt[rak[i]]++;
		for(int i=1;i<=n;i++) cnt[i]+=cnt[i-1];
		for(int i=n;i>=1;i--) sa[cnt[rak[tmp[i]]]--]=tmp[i];
		memcpy(rak1,rak,sizeof(rak));
		k=1;rak[sa[1]]=1;
		for(int i=2;i<=n;i++){
			if(rak1[sa[i]]!=rak1[sa[i-1]] || rak1[sa[i]]==rak1[sa[i-1]] && rak1[sa[i]+p]!=rak1[sa[i-1]+p] )k++;
			rak[sa[i]]=k;
		}
	}
	for(int i=1,k=0;i<=n;i++){
		if(rak[i]==n){
			h[rak[i]]=0;continue;
		}
		if(k) k--;
		while(s[i+k]==s[sa[rak[i]+1]+k]) k++;
		h[rak[i]]=k;
	}
	ans=n*(n+1)>>1;sum=0;
	for(int i=1;i<=n;i++){
		pa temp=mp(h[i],1);
		while(!S.empty() && S.top().x>h[i]){
			sum-=1ll*S.top().x*S.top().y;
			temp.y+=S.top().y;
			S.pop();
		}
		sum+=1ll*temp.x*temp.y;
		ans+=sum;
		S.push(temp);
	}
	printf("%I64d\n",ans);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
const double pi = acos(-1);
const int maxn = 2e6 + 10;
const double eps = 1e-8;
const ll mod = 1e9 + 7;
const ull base = 131;
const ull mod1 = 1e9 + 9;
const ull mod2 = 1610612741;

int sa[maxn];
int rk[maxn];
int tmp[maxn];
int lcp[maxn];
char s[maxn];
int n,k;
bool cmp(int i,int j)
{
    if(rk[i] != rk[j]) return rk[i]<rk[j];
    else
    {
        int ri = i+k<=n?rk[i+k]:-1;
        int rj = j+k<=n?rk[j+k]:-1;
        return ri < rj;
    }
}
void build(char *s,int *sa)
{
    n = strlen(s);
    for(int i = 0; i <= n; ++i){
        sa[i] = i;
        rk[i] = i<n?s[i]:-1;
    }
    for(k = 1; k <= n; k *= 2){
        sort(sa,sa+n+1,cmp);
        tmp[sa[0]]=0;
        for(int i = 1; i <= n; ++i)
            tmp[sa[i]]=tmp[sa[i-1]]+(cmp(sa[i-1],sa[i])?1:0);
        for(int i = 0; i <= n; ++i)
            rk[i]=tmp[i];
    }
}
void LCP(char *s,int *sa,int *lcp)
{
    n = strlen(s);
    for(int i = 0; i <= n; ++i) rk[sa[i]]=i;
    int h = 0;
    lcp[0] = 0;
    for(int i = 0; i < n; ++i)
    {
        int j = sa[rk[i]-1];
        for (h ? h-- : 0; j + h < n&&i + h < n&&s[j + h] == s[i + h]; h++);
        lcp[rk[i]-1] = h;
    }
}
struct node{
    ll x,y;
}st[maxn];
int main()
{
    scanf("%s",s);
    n=strlen(s);
    build(s,sa);
    LCP(s,sa,lcp);
    ll cnt = 0, ans = (ll)(n+1)*(ll)n/2, now = 0;
    for(int i = 0; i < n; ++i)
    {
        node tmp;
        tmp.x=lcp[i];
        tmp.y=1;
        while(now>0&&st[now].x>tmp.x){
            tmp.y+=st[now].y;
            cnt-=st[now].x*st[now].y;
            now--;
        }
        st[++now]=tmp;
        cnt+=tmp.x*tmp.y;
        ans+=cnt;
    }
    cout<<ans<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

const int N=1e5+10,M=2e5+10;
int tot=1,c[M][26],fa[M],len[M],sz[M],lst=1;
inline void insert(int ch){
	int p=lst,np=lst=++tot;
	len[np]=len[p]+1,sz[np]=1;
	for(;p&&!c[p][ch];p=fa[p])c[p][ch]=np;
	if(!p){fa[np]=1;return;}
	int q=c[p][ch];
	if(len[q]==len[p]+1){fa[np]=q;return;}
	int nq=++tot;
	memcpy(c[nq],c[q],sizeof(c[q]));
	fa[nq]=fa[q],fa[q]=fa[np]=nq;
	len[nq]=len[p]+1;
	for(;c[p][ch]==q;p=fa[p])c[p][ch]=nq;
}

int n,t[M];
char a[N];
inline bool cmp(int x,int y){return len[x]<len[y];}
inline ll C(ll x){return x*(x+1)/2;}
int main(){
	scanf("%s",a);
	n=strlen(a);
	for(int i=0;i<n;++i)insert(a[i]-97);
	for(int i=1;i<=tot;++i)t[i]=i;
	sort(t+1,t+tot+1,cmp);
	for(int i=tot;i;--i)sz[fa[t[i]]]+=sz[t[i]];
	ll ans=0;
	for(int i=1;i<=tot;++i)
		ans+=(len[i]-len[fa[i]])*C(sz[i]);
	printf("%I64d\n",ans);
}