#include <bits/stdc++.h>
using namespace std;
const long long maxn = 2e5 + 15;
inline long long read() {
  long long x = 0, f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = (x << 1) + (x << 3) + c - '0';
    c = getchar();
  }
  return x * f;
}
long long T, n, tot, last, len[maxn], size[maxn], link[maxn], nxt[maxn][30];
inline long long extend(long long last, long long x) {
  long long now = ++tot, pos = last, tmp, cln;
  len[now] = len[pos] + 1;
  size[now]++;
  while (pos != 0 && nxt[pos][x] == 0) nxt[pos][x] = now, pos = link[pos];
  if (!pos) {
    link[now] = 1;
    return now;
  }
  tmp = nxt[pos][x];
  if (len[tmp] == len[pos] + 1) {
    link[now] = tmp;
    return now;
  }
  cln = ++tot;
  len[cln] = len[pos] + 1;
  for (long long i = 1; i <= 26; i++) nxt[cln][i] = nxt[tmp][i];
  link[cln] = link[tmp];
  link[tmp] = link[now] = cln;
  while (pos != 0 && nxt[pos][x] == tmp) nxt[pos][x] = cln, pos = link[pos];
  return now;
}
string s;
long long beg[maxn], nex[maxn], to[maxn], e;
inline void add(long long x, long long y) {
  e++;
  nex[e] = beg[x];
  beg[x] = e;
  to[e] = y;
}
long long val[maxn], k;
inline void dfs(long long x) {
  for (long long i = beg[x]; i; i = nex[i]) dfs(to[i]), size[x] += size[to[i]];
  val[size[x]] += (len[x] - len[link[x]]);
}
inline void pre() {
  for (long long i = 0; i <= tot; i++) {
    val[i] = beg[i] = len[i] = size[i] = link[i] = 0;
    for (long long j = 1; j <= 26; j++) nxt[i][j] = 0;
  }
  e = tot = 0;
}
signed main() {
  T = read();
  while (T--) {
    pre();
    cin >> s;
    n = s.size();
    s = " " + s;
    last = ++tot;
    for (long long i = 1; i <= n; i++) last = extend(last, s[i] - 96);
    for (long long i = 2; i <= tot; i++) add(link[i], i);
    dfs(1);
    long long ans = 0;
    for (long long i = n; i >= 1; i--) ans += val[i] * i * i;
    printf("%lld\n", ans);
  }
  return 0;
}
