#include <bits/stdc++.h>
using std::vector;
const int N = 200010;
int _w;
int T, n;
char s[N];
namespace SAM {
int ch[N][26], nid;
int len[N], par[N], cnt[N];
vector<int> buc[N];
int append(int p, int c) {
  int np = nid++;
  len[np] = len[p] + 1;
  while (p != -1 && !ch[p][c]) ch[p][c] = np, p = par[p];
  if (p == -1) {
    par[np] = 0;
  } else {
    int q = ch[p][c];
    if (len[q] == len[p] + 1) {
      par[np] = q;
    } else {
      int nq = nid++;
      for (int i = 0; i < 26; ++i) ch[nq][i] = ch[q][i];
      par[nq] = par[q];
      len[nq] = len[p] + 1;
      par[q] = par[np] = nq;
      while (p != -1 && ch[p][c] == q) ch[p][c] = nq, p = par[p];
    }
  }
  return np;
}
void build(char *s) {
  memset(ch, 0, sizeof ch);
  nid = 1;
  len[0] = 0, par[0] = -1;
  int u = 0;
  for (; *s; ++s) u = append(u, *s - 'a');
}
void solve(char *s) {
  memset(cnt, 0, sizeof cnt);
  int u = 0;
  for (; *s; ++s) {
    u = ch[u][*s - 'a'];
    ++cnt[u];
  }
  for (int i = 0; i <= n; ++i) buc[i].clear();
  for (int i = 1; i < nid; ++i) buc[len[i]].push_back(i);
  for (int i = n; i; --i)
    for (int j = 0; j < (int)buc[i].size(); ++j) {
      int u = buc[i][j];
      cnt[par[u]] += cnt[u];
    }
  long long ans = 0;
  for (int i = 1; i < nid; ++i)
    ans += 1LL * cnt[i] * cnt[i] * (len[i] - len[par[i]]);
  printf("%lld\n", ans);
}
}  // namespace SAM
int main() {
  _w = scanf("%d", &T);
  while (T--) {
    _w = scanf("%s", s);
    n = (int)strlen(s);
    SAM::build(s);
    SAM::solve(s);
  }
  return 0;
}
