#include <bits/stdc++.h>
using namespace std;
struct suffix {
  long long ind;
  long long rank[2];
};
bool comp(suffix a, suffix b) {
  if (a.rank[0] == b.rank[0]) return a.rank[1] < b.rank[1];
  return a.rank[0] < b.rank[0];
}
void calsuffixarray(string s, vector<long long> &suffixarr) {
  long long n;
  n = s.size();
  vector<suffix> suffixes(n);
  for (int i = 0; i < n; i++) {
    suffixes[i].ind = i;
    suffixes[i].rank[0] = s[i] - 'a';
    suffixes[i].rank[1] = ((i + 1) < n) ? s[i + 1] - 'a' : -1;
  }
  sort(suffixes.begin(), suffixes.end(), comp);
  vector<long long> ind(n);
  for (long long k = 4; k < 2 * n; k = k * 2) {
    long long rank = 0;
    long long prev_rank = suffixes[0].rank[0];
    suffixes[0].rank[0] = rank;
    ind[suffixes[0].ind] = 0;
    for (int i = 1; i < n; i++) {
      if (suffixes[i].rank[0] == prev_rank &&
          suffixes[i].rank[1] == suffixes[i - 1].rank[1]) {
        prev_rank = suffixes[i].rank[0];
        suffixes[i].rank[0] = rank;
      } else {
        prev_rank = suffixes[i].rank[0];
        suffixes[i].rank[0] = ++rank;
      }
      ind[suffixes[i].ind] = i;
    }
    for (int i = 0; i < n; i++) {
      long long nextindex = suffixes[i].ind + k / 2;
      suffixes[i].rank[1] =
          (nextindex < n) ? suffixes[ind[nextindex]].rank[0] : -1;
    }
    sort(suffixes.begin(), suffixes.end(), comp);
  }
  for (int i = 0; i < n; i++) {
    suffixarr[i] = suffixes[i].ind;
  }
}
void callcparray(string s, vector<long long> &suffixarr,
                 vector<long long> &lcp) {
  long long n = suffixarr.size();
  vector<long long> invsuff(n, 0);
  for (int i = 0; i < n; i++) {
    invsuff[suffixarr[i]] = i;
  }
  long long k = 0;
  for (int i = 0; i < n; i++) {
    if (invsuff[i] == n - 1) {
      k = 0;
      continue;
    }
    long long j = suffixarr[invsuff[i] + 1];
    while (i + k < n && j + k < n && s[i + k] == s[j + k]) {
      k++;
    }
    lcp[invsuff[i]] = k;
    if (k > 0) k--;
  }
}
const int N = 1e5 + 5;
const int LN = 18;
int t;
char str[N];
int n;
int sa[LN][N];
int arr[N];
int lcp[N];
pair<pair<int, int>, int> tmp[N];
void buildsa() {
  for (int i = 1; i <= n; ++i) {
    sa[0][i] = str[i] - 'a' + 1;
  }
  for (int i = 1; i < LN; ++i) {
    for (int j = 1; j <= n; ++j) {
      int x = j + (1 << (i - 1));
      tmp[j] = {{sa[i - 1][j], (x > n) ? -1 : sa[i - 1][x]}, j};
    }
    sort(tmp + 1, tmp + 1 + n);
    sa[i][tmp[1].second] = 1;
    for (int j = 2; j <= n; ++j) {
      sa[i][tmp[j].second] =
          sa[i][tmp[j - 1].second] + (tmp[j].first != tmp[j - 1].first);
    }
  }
  for (int i = 1; i <= n; ++i) {
    arr[sa[LN - 1][i]] = i;
  }
  for (int i = 1; i < n; ++i) {
    int x = arr[i];
    int y = arr[i + 1];
    int res = 0;
    for (int j = LN - 1; j >= 0; --j) {
      if (sa[j][x] == sa[j][y]) {
        res += 1 << j;
        x += 1 << j;
        y += 1 << j;
      }
    }
    lcp[i] = res;
  }
  lcp[n] = 0;
}
int lft[N];
int rgt[N];
void solve() {
  string st;
  cin >> st;
  strcpy(str + 1, st.c_str());
  n = strlen(str + 1);
  vector<long long> suf(n + 1);
  st = "$" + st;
  calsuffixarray(st, suf);
  vector<long long> lcp(n + 1);
  callcparray(st, suf, lcp);
  long long ans = 1LL * n * (n + 1LL);
  ans >>= 1;
  stack<int> s;
  while (!s.empty()) {
    s.pop();
  }
  for (int i = 1; i < n; ++i) {
    while (!s.empty() && lcp[s.top()] >= lcp[i]) {
      s.pop();
    }
    if (s.empty()) {
      lft[i] = 1;
    } else {
      lft[i] = s.top() + 1;
    }
    s.push(i);
  }
  while (!s.empty()) {
    s.pop();
  }
  for (int i = n - 1; i >= 1; --i) {
    while (!s.empty() && lcp[s.top()] > lcp[i]) {
      s.pop();
    }
    if (s.empty()) {
      rgt[i] = n;
    } else {
      rgt[i] = s.top();
    }
    s.push(i);
  }
  for (int i = 1; i < n; ++i) {
    ans += 2LL * lcp[i] * (i - lft[i] + 1) * (rgt[i] - i);
  }
  printf("%lld\n", ans);
}
int main() {
  scanf("%d", &t);
  while (t--) {
    solve();
  }
}
