#include <bits/stdc++.h>
const int mod = 998244353;
using namespace std;
inline int read() {
  int f = 1, x = 0;
  char s = getchar();
  while (!isdigit(s)) {
    if (s == '-') f = -1;
    s = getchar();
  }
  while (isdigit(s)) {
    x = (x << 1) + (x << 3) + (s ^ 48), s = getchar();
  }
  return x * f;
}
int t, strl;
long long ans;
char str[2010000];
struct Suffix_Tree {
  int ch[2010000][27], link[2010000], len[2010000], start[2010000], s[2010000],
      rem, tot, siz[2010000], n, now;
  inline void reset(void) {
    memset(ch[tot], 0, sizeof(ch[tot]));
    while (tot--) memset(ch[tot], 0, sizeof(ch[tot]));
    n = rem = 0, tot = 1, len[0] = 0x3f3f3f3f, now = 1;
  }
  inline int New(int st, int le) {
    link[++tot] = 1, start[tot] = st, len[tot] = le;
    return tot;
  }
  inline void extend(int x) {
    s[++n] = x, rem++;
    for (int last = 1; rem;) {
      while (rem > len[ch[now][s[n - rem + 1]]])
        rem -= len[now = ch[now][s[n - rem + 1]]];
      int &v = ch[now][s[n - rem + 1]], c = s[start[v] + rem - 1];
      if (!v || x == c) {
        link[last] = now, last = now;
        if (!v)
          v = New(n - rem + 1, 0x3f3f3f3f);
        else
          break;
      } else {
        int u = New(start[v], rem - 1);
        ch[u][c] = v, ch[u][x] = New(n, 0x3f3f3f3f);
        start[v] += rem - 1, len[v] -= rem - 1;
        link[last] = v = u, last = u;
      }
      if (now == 1)
        rem--;
      else
        now = link[now];
    }
  }
  inline void adjust(int x) {
    int cnt = 0;
    siz[x] = 0;
    for (int i = 0; i <= 26; i++)
      if (ch[x][i]) ++cnt, adjust(ch[x][i]), siz[x] += siz[ch[x][i]];
    if (!cnt) len[x] = strl - start[x] + 1, siz[x] = 1;
  }
  inline void dfs(int x, int dep) {
    int cnt = 0;
    ans += 1ll * siz[x] * siz[x] * len[x];
    for (int i = 0; i <= 26; i++)
      if (ch[x][i]) ++cnt, dfs(ch[x][i], dep + len[ch[x][i]]);
  }
} tree;
signed main(void) {
  t = read();
  while (t--) {
    scanf("%s", str + 1);
    strl = strlen(str + 1);
    tree.reset(), ans = 0;
    for (int i = 1; i <= strl; i++) tree.extend(str[i] - 'a');
    tree.extend(26), tree.adjust(1);
    tree.dfs(1, 0), printf("%lld\n", ans);
  }
  return 0;
}
