#include <bits/stdc++.h>
const int S = 400004;
int last, tr[S][26], fail[S], len[S], tot = 0, eg = 1, h[S], nx[S << 1],
                                      v[S << 1], e[S];
char c[S];
void insert(int &last, char c) {
  c -= 'a';
  int p = last, now = ++tot;
  len[now] = len[last] + 1;
  while (p != -1 && !tr[p][c]) {
    tr[p][c] = now;
    p = fail[p];
  }
  if (p == -1)
    fail[now] = 0;
  else {
    int q = tr[p][c];
    if (len[p] + 1 == len[q])
      fail[now] = q;
    else {
      int w = ++tot;
      len[w] = len[p] + 1;
      fail[w] = fail[q];
      for (int i = 0; i < 26; ++i) tr[w][i] = tr[q][i];
      fail[q] = fail[now] = w;
      while (p != -1 && tr[p][c] == q) {
        tr[p][c] = w;
        p = fail[p];
      }
    }
  }
  last = now;
}
inline void egadd(int uu, int vv) {
  nx[++eg] = h[uu];
  h[uu] = eg;
  v[eg] = vv;
}
long long res = 0;
void dfs(int x) {
  for (int i = h[x]; i; i = nx[i]) {
    dfs(v[i]);
    e[x] += e[v[i]];
  }
  res += (len[x] - len[fail[x]]) * 1ll * e[x] * e[x];
}
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    tot = last = 0;
    fail[0] = -1;
    res = 0;
    scanf("%s", c + 1);
    for (int i = 1; c[i]; ++i) insert(last, c[i]);
    int p = 0;
    for (int i = 1; c[i]; ++i) {
      p = tr[p][c[i] - 'a'];
      e[p] = 1;
    }
    for (int i = 1; i <= tot; ++i) egadd(fail[i], i);
    dfs(0);
    printf("%lld\n", res);
    for (int i = 0; i <= tot; ++i) {
      fail[i] = len[i] = 0;
      h[i] = 0;
      e[i] = 0;
      for (int j = 0; j < 26; ++j) tr[i][j] = 0;
    }
    eg = 1;
  }
  return 0;
}
