#include <bits/stdc++.h>
using namespace std;
int const MAXN = 2e5 + 10;
char s[MAXN];
int n, m;
struct SAM {
  int trans[MAXN << 1][26], link[MAXN << 1], len[MAXN << 1], siz[MAXN];
  int Cnt = 1;
  int insert(int c, int lst) {
    int np = ++Cnt, p = lst;
    len[np] = len[p] + 1;
    siz[np] = 1;
    for (; p && !trans[p][c]; p = link[p]) trans[p][c] = np;
    if (!p)
      link[np] = 1;
    else {
      int q = trans[p][c];
      if (len[q] == len[p] + 1)
        link[np] = q;
      else {
        int clone = ++Cnt;
        len[clone] = len[p] + 1;
        link[clone] = link[q];
        link[q] = link[np] = clone;
        memcpy(trans[clone], trans[q], sizeof(trans[q]));
        for (; p && trans[p][c] == q; p = link[p]) trans[p][c] = clone;
      }
    }
    return np;
  }
  int Rank[MAXN << 1], Bin[MAXN << 1];
  long long ans = 0;
  void run() {
    ans = 0;
    for (int i = 1; i <= Cnt; i++) Bin[len[i]]++;
    for (int i = 1; i <= Cnt; i++) Bin[i] += Bin[i - 1];
    for (int i = 1; i <= Cnt; i++) Rank[Bin[len[i]]--] = i;
    for (int i = Cnt; i >= 1; i--) {
      int p = Rank[i];
      siz[link[p]] += siz[p];
      ans += 1ll * siz[p] * siz[p] * (len[p] - len[link[p]]);
    }
    printf("%lld\n", ans);
  }
  void init() {
    memset(trans, 0, sizeof(trans));
    memset(link, 0, sizeof(link));
    memset(len, 0, sizeof(len));
    memset(siz, 0, sizeof(siz));
    Cnt = 1;
    ans = 0;
    memset(Rank, 0, sizeof(Rank));
    memset(Bin, 0, sizeof(Bin));
  }
} sam;
void work() {
  scanf("%s", s + 1);
  sam.init();
  int len = strlen(s + 1);
  for (int i = 1, lst = 1; i <= len; i++) {
    lst = sam.insert(s[i] - 'a', lst);
  }
  sam.run();
}
int main() {
  int T;
  scanf("%d", &T);
  for (int i = 1; i <= T; i++) {
    work();
  }
  return 0;
}
