#include <bits/stdc++.h>
using namespace std;
char s[1000005];
int b[1000005];
struct sam_point {
  int fa, son[30];
  long long sum, len;
  void clear() {
    fa = len = sum = 0;
    for (int i = 0; i < 30; i++) son[i] = 0;
  }
};
struct SAM {
  int n, last, len, hea[1000005], nex[1000005], wen[1000005];
  sam_point a[1000005];
  long long ans;
  void clear() {
    for (int i = 1; i <= n; i++) a[i].clear(), hea[i] = 0;
    ans = len = 0;
    n = last = 1;
  }
  void add(int x, int y) {
    ++len;
    nex[len] = hea[x];
    wen[len] = y;
    hea[x] = len;
  }
  void insert(int x) {
    int p1 = last, p2 = ++n;
    last = n;
    a[p2].sum = 1;
    a[p2].len = a[p1].len + 1;
    for (; p1 && !a[p1].son[x]; p1 = a[p1].fa) a[p1].son[x] = p2;
    if (!p1)
      a[p2].fa = 1;
    else {
      int q1 = a[p1].son[x];
      if (a[p1].len + 1 == a[q1].len)
        a[p2].fa = q1;
      else {
        int q2 = ++n;
        a[q2] = a[q1];
        a[q2].sum = 0;
        a[q2].len = a[p1].len + 1;
        a[q1].fa = a[p2].fa = q2;
        for (; p1 && a[p1].son[x] == q1; p1 = a[p1].fa) a[p1].son[x] = q2;
      }
    }
  }
  void build() {
    for (int i = 2; i <= n; i++) add(a[i].fa, i);
  }
  void dfs(int x) {
    for (int i = hea[x]; i; i = nex[i]) {
      dfs(wen[i]);
      a[x].sum += a[wen[i]].sum;
    }
    ans += a[x].sum * a[x].sum * (a[x].len - a[a[x].fa].len);
  }
} sam;
int main() {
  int t;
  scanf("%d", &t);
  do {
    sam.clear();
    scanf("%s", s);
    int len = strlen(s);
    for (int i = 1; i <= len; i++) b[i] = 0;
    for (int i = 0; i <= len - 1; i++) sam.insert(s[i] - 97);
    sam.build();
    sam.dfs(1);
    printf("%lld\n", sam.ans);
    --t;
  } while (t);
}
