#include <bits/stdc++.h>
using namespace std;
const long long N = 300 * 1000 + 20, MOD = 1e9 + 7, base = 727;
long long hsh[N], n, pw[N], a[N], ca[N], L[N], R[N], sum, tot, tmp, _lcp[N];
string s, b;
bool add[N];
vector<long long> vec[N];
inline long long get_hash(long long l, long long r) {
  return hsh[r] - hsh[l] * pw[r - l];
}
inline long long lcp(long long i, long long j) {
  long long l = 0, r = n - max(i, j) + 1;
  while (l + 1 < r) {
    long long mid = (l + r) / 2;
    if (get_hash(i, i + mid) == get_hash(j, j + mid))
      l = mid;
    else
      r = mid;
  }
  return l;
}
inline bool cmp(long long i, long long j) {
  long long l = lcp(i, j);
  if (l + max(i, j) == n)
    return i > j;
  else
    return s[i + l] < s[j + l];
}
inline void _union(long long i) {
  long long x = L[i], y = L[i + 1];
  sum -= ca[x] * ca[x] + ca[y] * ca[y];
  tmp -= ca[x] + ca[y];
  if (ca[x] == 0) ca[x] = 1;
  if (ca[y] == 0) ca[y] = 1;
  ca[L[i]] += ca[L[i + 1]];
  ca[R[i + 1]] = ca[L[i]];
  R[L[i]] = R[i + 1];
  L[R[i + 1]] = L[i];
  sum += ca[x] * ca[x];
  tmp += ca[x];
  return;
}
inline void solve() {
  cin >> s;
  n = s.size();
  long long M = n + 1;
  iota(a, a + M, 0);
  iota(L, L + M, 0);
  iota(R, R + M, 0);
  fill(ca, ca + M, 0);
  for (long long i = 0; i < n; i++) {
    hsh[i + 1] = hsh[i] * base + s[i];
  }
  sort(a, a + n, cmp);
  for (long long i = 0; i < n - 1; i++) _lcp[i] = lcp(a[i], a[i + 1]);
  {
    stack<long long> s;
    s.push(-1);
    tot = sum = 0;
    long long al = (n * (n + 1)) / 2;
    for (long long i = 0; i < n - 1; i++) {
      while (s.top() != -1 && _lcp[s.top()] >= _lcp[i]) {
        long long id = s.top();
        s.pop();
        sum -= _lcp[id] * (id - s.top());
      }
      sum += _lcp[i] * (i - s.top());
      tot += sum;
      s.push(i);
    }
    tot *= 2;
    cout << tot + al << '\n';
  }
  return;
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(nullptr);
  cout.tie(nullptr);
  ;
  for (long long i = pw[0] = 1; i < N; i++) pw[i] = pw[i - 1] * base;
  long long q;
  cin >> q;
  while (q--) solve();
  return 0;
}
