#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10, size = 1 << 20;
template <class o>
void qr(o &x) {
  char c = getchar();
  x = 0;
  int f = 1;
  while (!isdigit(c)) {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (isdigit(c)) x = x * 10 + c - '0', c = getchar();
  x *= f;
}
template <class o>
void qw(o x) {
  if (x / 10) qw(x / 10);
  putchar(x % 10 + '0');
}
template <class o>
void pr1(o x) {
  if (x < 0) x = -x, putchar('-');
  qw(x);
  putchar(' ');
}
template <class o>
void pr2(o x) {
  if (x < 0) x = -x, putchar('-');
  qw(x);
  puts("");
}
int T, n, last, tot, f[N], b[N], c[N];
char s[N];
long long ans;
struct node {
  int fa, len, v[26];
} tr[N];
void add(int c) {
  int p = last, x = last = ++tot;
  tr[x].len = tr[p].len + 1;
  for (; p && tr[p].v[c] == 0; p = tr[p].fa) tr[p].v[c] = x;
  if (!p)
    tr[x].fa = 1;
  else {
    int q = tr[p].v[c], y;
    if (tr[p].len + 1 == tr[q].len)
      tr[x].fa = q;
    else {
      tr[y = ++tot] = tr[q];
      tr[y].len = tr[p].len + 1;
      tr[q].fa = tr[x].fa = y;
      for (; p && tr[p].v[c] == q; p = tr[p].fa) tr[p].v[c] = y;
    }
  }
}
void init() {
  memset(tr + 1, 0, tot * 28 << 2);
  memset(f + 1, 0, tot << 2);
  memset(c, 0, n + 1 << 2);
  last = tot = 1;
  ans = n = 0;
}
int main() {
  qr(T);
  while (T--) {
    init();
    scanf("%s", s + 1);
    for (int i = 1; s[i]; i++) n++, add(s[i] -= 'a'), f[last]++;
    for (int i = 1; i <= tot; i++) c[tr[i].len]++;
    for (int i = 1; i <= n; i++) c[i] += c[i - 1];
    for (int i = tot; i; i--) b[c[tr[i].len]--] = i;
    for (int i = tot, j; i; i--) {
      j = b[i];
      f[tr[j].fa] += f[j];
      ans += (long long)f[j] * f[j] * (tr[j].len - tr[tr[j].fa].len);
    }
    pr2(ans);
  }
  return 0;
}
