#include <bits/stdc++.h>
const int kN = 1e5 + 10;
int lth;
char s[kN];
inline int read() {
  int f = 1, w = 0;
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-') f = -1;
  for (; isdigit(ch); ch = getchar()) w = (w << 3) + (w << 1) + (ch ^ '0');
  return f * w;
}
void Chkmax(int &fir, int sec) {
  if (sec > fir) fir = sec;
}
void Chkmin(int &fir, int sec) {
  if (sec < fir) fir = sec;
}
namespace SAM {
const int kNode = kN << 1;
int node_num, last, link[kNode], len[kNode], sz[kNode], tr[kNode][26];
int id[kNode], cnt[kN];
void Init() {
  for (int i = 1; i <= lth; ++i) cnt[i] = 0;
  for (int i = 1; i <= node_num; ++i) {
    for (int j = 0; j < 26; ++j) tr[i][j] = 0;
    link[i] = id[i] = sz[i] = 0;
  }
  node_num = last = 1;
}
void Insert(int ch_) {
  int p = last, now_ = last = ++node_num;
  sz[now_] = 1, len[now_] = len[p] + 1;
  for (; p && !tr[p][ch_]; p = link[p]) tr[p][ch_] = now_;
  if (!p) {
    link[now_] = 1;
    return;
  }
  int q = tr[p][ch_];
  if (len[q] == len[p] + 1) {
    link[now_] = q;
    return;
  }
  int newq = ++node_num;
  len[newq] = len[p] + 1;
  link[newq] = link[q];
  link[q] = link[now_] = newq;
  memcpy(tr[newq], tr[q], sizeof(tr[q]));
  for (; p && tr[p][ch_] == q; p = link[p]) tr[p][ch_] = newq;
}
long long Query() {
  for (int i = 1; i <= node_num; ++i) ++cnt[len[i]];
  for (int i = 1; i <= lth; ++i) cnt[i] += cnt[i - 1];
  for (int i = node_num; i >= 1; --i) id[--cnt[len[i]]] = i;
  long long ans = 0;
  for (int i = node_num; i >= 1; --i) {
    sz[link[id[i]]] += sz[id[i]];
    ans += 1ll * sz[id[i]] * sz[id[i]] * (len[id[i]] - len[link[id[i]]]);
  }
  return ans;
}
}  // namespace SAM
int main() {
  int T = read();
  while (T--) {
    SAM::Init();
    scanf("%s", s + 1);
    lth = strlen(s + 1);
    for (int i = 1; i <= lth; ++i) SAM::Insert(s[i] - 'a');
    printf("%lld\n", SAM::Query());
  }
  return 0;
}
