#include <bits/stdc++.h>
using namespace std;
const int EE = 1200000;
char a[EE];
struct tree {
  int fa, len;
  int ch[30];
  tree() {
    memset(ch, 0, sizeof(ch));
    len = fa = 0;
  }
} tree[EE];
int top = 1, last = 1;
int fir[EE], nxt[EE << 1], to[EE << 1], ztop = 0;
long long aans[EE];
long long ans = 0;
void add(int qap) {
  int p = last, np = last = ++top;
  aans[top] = 1;
  tree[np].len = tree[p].len + 1;
  while (p && !tree[p].ch[qap]) tree[p].ch[qap] = np, p = tree[p].fa;
  if (!p) {
    tree[np].fa = 1;
    return;
  }
  int q = tree[p].ch[qap];
  if (tree[q].len == tree[p].len + 1) {
    tree[np].fa = q;
    return;
  }
  int nq = ++top;
  tree[nq] = tree[q];
  tree[nq].len = tree[p].len + 1;
  tree[q].fa = tree[np].fa = nq;
  while (p && tree[p].ch[qap] == q) tree[p].ch[qap] = nq, p = tree[p].fa;
}
int maxx(int x, int y) { return x > y ? x : y; }
void addition(int x, int y) {
  nxt[++ztop] = fir[x], fir[x] = ztop, to[ztop] = y;
}
void dfs(int x, int fa) {
  for (int i = fir[x]; i; i = nxt[i]) {
    dfs(to[i], x);
    aans[x] += aans[to[i]];
  }
  ans += aans[x] * aans[x] * (tree[x].len - tree[fa].len);
}
void csh() {
  memset(fir, 0, sizeof(fir));
  memset(nxt, 0, sizeof(nxt));
  memset(to, 0, sizeof(to));
  memset(tree, 0, sizeof(tree));
  memset(aans, 0, sizeof(aans));
  memset(a, 0, sizeof(a));
  top = last = 1;
  ztop = ans = 0;
}
void sol() {
  csh();
  scanf("%s", a);
  int llen = strlen(a);
  int zz = 0;
  while (zz < llen) add(a[zz++] - 'a' + 1);
  for (int i = 2; i <= top; i++) addition(tree[i].fa, i);
  dfs(1, 1);
  printf("%lld\n", ans);
}
signed main() {
  int T;
  cin >> T;
  while (T) {
    T--;
    sol();
  }
}
