#include <bits/stdc++.h>
template <typename T>
void read(T& x) {
  x = 0;
  int flag = 1;
  char c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-') flag = -1;
  for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
  x *= flag;
}
using namespace std;
const int maxn = 200010;
int n;
char s[maxn];
struct SAM {
  int fa[maxn], len[maxn], ch[maxn][26], sz[maxn], cnt, lst;
  bool vis[maxn];
  vector<int> e[maxn];
  void clear() {
    for (int i = 0; i <= (int)2 * n + 1; i++) {
      fa[i] = -1, len[i] = sz[i] = vis[i] = 0;
      memset(ch[i], 0, sizeof(ch[i]));
      e[i].clear();
    }
    cnt = lst = 0;
  }
  void ins(int c) {
    int p = lst, u = ++cnt;
    len[u] = len[p] + 1, sz[u] = 1;
    for (; ~p && !ch[p][c]; p = fa[p]) ch[p][c] = u;
    if (p == -1)
      fa[u] = 0;
    else {
      int q = ch[p][c];
      if (len[p] + 1 == len[q])
        fa[u] = q;
      else {
        int nq = ++cnt;
        memcpy(ch[nq], ch[q], sizeof(ch[nq]));
        fa[nq] = fa[q], len[nq] = len[p] + 1;
        fa[u] = fa[q] = nq;
        for (; ~p && ch[p][c] == q; p = fa[p]) ch[p][c] = nq;
      }
    }
    lst = u;
  }
  long long res;
  void dfs(int u) {
    vis[u] = 1;
    for (int i = 0; i <= (int)e[u].size() - 1; i++) {
      if (!vis[e[u][i]]) dfs(e[u][i]);
      sz[u] += sz[e[u][i]];
    }
    int tmp = len[u] - len[fa[u]];
    res += 1ll * sz[u] * sz[u] * tmp;
  }
  void solve() {
    res = 0;
    for (int i = 1; i <= (int)cnt; i++) e[fa[i]].push_back(i);
    for (int i = 0; i <= (int)cnt; i++)
      if (!vis[i]) dfs(i);
    printf("%lld\n", res);
  }
} qwq;
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    scanf("%s", s + 1);
    n = strlen(s + 1);
    qwq.clear();
    for (int i = 1; i <= (int)n; i++) qwq.ins(s[i] - 'a');
    qwq.solve();
  }
  return 0;
}
