#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
char s[maxn];
int n;
long long ans;
namespace SAM {
int fa[maxn], ch[maxn][26], mxlen[maxn], cend[maxn], cnode, root, lst;
vector<int> g[maxn];
vector<int> len[maxn];
inline int getnewnode() {
  fa[cnode] = -1;
  memset(ch[cnode], -1, sizeof(ch[0]));
  cend[cnode] = 0;
  return cnode++;
}
void init(int m) {
  for (int i = 0; i <= m; i++) len[i].clear();
  cnode = 0;
  lst = root = getnewnode();
  mxlen[root] = 0;
}
void extend(int c) {
  int cur = getnewnode();
  mxlen[cur] = mxlen[lst] + 1;
  len[mxlen[cur]].push_back(cur);
  cend[cur]++;
  int p;
  for (p = lst; p != -1 && ch[p][c] == -1; p = fa[p]) ch[p][c] = cur;
  lst = cur;
  if (p == -1) {
    fa[cur] = root;
    return;
  }
  int q = ch[p][c];
  if (mxlen[q] == mxlen[p] + 1) {
    fa[cur] = q;
    return;
  }
  int clone = getnewnode();
  mxlen[clone] = mxlen[p] + 1;
  len[mxlen[clone]].push_back(clone);
  fa[clone] = fa[q];
  for (int i = 0; i < 26; i++) ch[clone][i] = ch[q][i];
  fa[q] = fa[cur] = clone;
  for (; p != -1 && ch[p][c] == q; p = fa[p]) ch[p][c] = clone;
}
void build() {
  for (int i = 0; i < n; i++) extend(s[i] - 'a');
  for (int l = n; l >= 1; l--) {
    for (int i = 0; i < len[l].size(); i++) {
      int x = len[l][i];
      int cnt = mxlen[x] - mxlen[fa[x]];
      cend[fa[x]] += cend[x];
      ans += cnt * 1ll * cend[x] * 1ll * cend[x];
    }
  }
}
}  // namespace SAM
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    scanf("%s", s);
    n = strlen(s);
    ans = 0;
    SAM::init(n);
    SAM::build();
    printf("%I64d\n", ans);
  }
  return 0;
}
