#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 7;
struct SAM {
  struct node {
    int son[26], pre, len;
  } a[N << 1];
  int f[N << 1];
  vector<int> e[N << 1];
  int tot, tail = 0;
  void init() {
    for (int i = int(0); i <= int(tot); ++i) {
      e[i].clear(), f[i] = 0;
      memset(a[i].son, 0, sizeof(a[i].son));
      a[i].pre = a[i].len = 0;
    }
    tot = tail = 0;
  }
  void ins(int k) {
    int p, q, np = ++tot, nq;
    a[np].len = a[tail].len + 1;
    f[np] = 1;
    for (p = tail; p && !a[p].son[k]; p = a[p].pre) a[p].son[k] = np;
    if (!a[p].son[k]) {
      a[p].son[k] = np;
      a[np].pre = p;
    } else {
      q = a[p].son[k];
      if (a[q].len == a[p].len + 1)
        a[np].pre = q;
      else {
        a[nq = ++tot] = a[q];
        a[nq].len = a[p].len + 1;
        a[q].pre = a[np].pre = nq;
        for (; a[p].son[k] == q; p = a[p].pre) a[p].son[k] = nq;
      }
    }
    tail = np;
  }
  void dfs(int u) {
    for (int v : e[u]) dfs(v);
    f[a[u].pre] += f[u];
  }
  void solve() {
    for (int i = int(1); i <= int(tot); ++i) e[a[i].pre].push_back(i);
    dfs(0);
    long long ans = 0;
    for (int i = int(1); i <= int(tot); ++i) {
      ans += 1ll * (a[i].len - a[a[i].pre].len) * f[i] * f[i];
    }
    printf("%lld\n", ans);
  }
} sam;
char s[N];
int main() {
  int _;
  scanf("%d", &_);
  while (_--) {
    sam.init();
    scanf("%s", s);
    int len = strlen(s);
    for (int i = int(0); i <= int(len - 1); ++i) sam.ins(s[i] - 'a');
    sam.solve();
  }
}
