#include <bits/stdc++.h>
using namespace std;
int T, n, ch[400005][26], fa[400005], len[400005], cnt[400005], las = 1,
                                                                tot = 1;
char s[400005];
struct edge {
  int to, nxt;
} es[400005];
int Tot, hd[400005];
void add(int u, int v) {
  es[++Tot] = (edge){v, hd[u]};
  hd[u] = Tot;
}
void extend(int c) {
  int p = las, np = las = ++tot;
  cnt[np] = 1;
  len[np] = len[p] + 1;
  for (; p && !ch[p][c]; p = fa[p]) ch[p][c] = np;
  if (!p)
    fa[np] = 1;
  else {
    int q = ch[p][c];
    if (len[q] == len[p] + 1)
      fa[np] = q;
    else {
      int nq = ++tot;
      fa[nq] = fa[q];
      memcpy(ch[nq], ch[q], sizeof(ch[nq]));
      len[nq] = len[p] + 1;
      fa[q] = fa[np] = nq;
      for (; p && ch[p][c] == q; p = fa[p]) ch[p][c] = nq;
    }
  }
}
void dfs(int u) {
  for (int i = hd[u], v; i; i = es[i].nxt) {
    dfs(v = es[i].to);
    cnt[u] += cnt[v];
  }
}
int main() {
  scanf("%d", &T);
  while (T--) {
    Tot = 0;
    memset(hd, 0, sizeof(hd));
    for (int i = 1; i <= tot; ++i)
      fa[i] = len[i] = cnt[i] = 0, memset(ch[i], 0, sizeof(ch[i]));
    las = tot = 1;
    scanf("%s", s);
    n = strlen(s);
    for (int i = 0; i < n; ++i) extend(s[i] - 'a');
    for (int i = 2; i <= tot; ++i) add(fa[i], i);
    dfs(1);
    long long ans = 0;
    for (int i = 2; i <= tot; ++i)
      ans += (long long)cnt[i] * cnt[i] * (len[i] - len[fa[i]]);
    printf("%lld\n", ans);
  }
  return 0;
}
