#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline void Read(T &n) {
  char ch;
  bool flag = false;
  while (!isdigit(ch = getchar()))
    if (ch == '-') flag = true;
  for (n = ch ^ 48; isdigit(ch = getchar());
       n = (n << 1) + (n << 3) + (ch ^ 48))
    ;
  if (flag) n = -n;
}
const int MAXN = 100005;
struct node {
  int son[26], fa, len, sz;
} t[MAXN << 1];
int node_cnt = 1;
int prv = 1;
inline void insert(int v) {
  int x = ++node_cnt;
  t[x].len = t[prv].len + 1;
  t[x].sz = 1;
  while (prv and !t[prv].son[v]) t[prv].son[v] = x, prv = t[prv].fa;
  if (!prv)
    t[x].fa = 1;
  else {
    int p = t[prv].son[v];
    if (t[p].len == t[prv].len + 1)
      t[x].fa = p;
    else {
      int new_p = ++node_cnt;
      t[new_p].len = t[prv].len + 1;
      t[new_p].fa = t[p].fa;
      t[p].fa = t[x].fa = new_p;
      copy(t[p].son, t[p].son + 26, t[new_p].son);
      while (prv and t[prv].son[v] == p) t[prv].son[v] = new_p, prv = t[prv].fa;
    }
  }
  prv = x;
}
char s[MAXN];
int S[MAXN], id[MAXN << 1];
int main() {
  int T;
  Read(T);
  while (T--) {
    scanf("%s", s + 1);
    int n = strlen(s + 1);
    prv = node_cnt = 1;
    memset(t, 0, sizeof t);
    memset(S, 0, sizeof S);
    for (register int i = 1; i <= n; i++) insert(s[i] - 'a');
    for (register int i = 1; i <= node_cnt; i++) S[t[i].len]++;
    for (register int i = 1; i <= n; i++) S[i] += S[i - 1];
    for (register int i = 1; i <= node_cnt; i++) id[S[t[i].len]--] = i;
    for (register int i = node_cnt; i; i--) t[t[id[i]].fa].sz += t[id[i]].sz;
    long long ans = 0;
    for (register int i = 2; i <= node_cnt; i++)
      ans += 1ll * t[i].sz * t[i].sz * (t[i].len - t[t[i].fa].len);
    printf("%lld\n", ans);
  }
  return 0;
}
