#include <bits/stdc++.h>
using namespace std;
long long int inf = 1000000ll * 1000 + 200;
long long int mod = 1e9 + 7;
const int maxn = 2e5 + 20;
vector<long long int> vec[maxn];
long long int hsh[maxn], p[maxn];
long long int cnt[maxn], l[maxn], r[maxn], res, sufarr[maxn], alive[maxn];
string s;
int n;
long long int gthsh(int l, int r) {
  long long int res = (hsh[r] - hsh[l - 1] * p[r - l + 1]);
  return res;
}
int lcp(int i, int j) {
  int l = 0, r = n - max(i, j) + 2;
  while (r - l > 1) {
    int mid = (l + r) >> 1;
    if (gthsh(i, i + mid - 1) == gthsh(j, j + mid - 1)) {
      l = mid;
    } else {
      r = mid;
    }
  }
  return l;
}
bool cmp(int i, int j) {
  int tmp = lcp(i, j);
  return (int(s[i + tmp - 1]) < int(s[j + tmp - 1]));
}
void uni(int x, int y) {
  res -= cnt[x] * cnt[x] + cnt[y] * cnt[y];
  res += (cnt[x] + cnt[y]) * (cnt[x] + cnt[y]);
  l[r[y]] = l[x];
  r[l[x]] = r[y];
  long long int tmp = cnt[x];
  cnt[l[x]] += cnt[y];
  cnt[r[y]] += tmp;
  return;
}
void solve() {
  long long int ans = 0;
  res = 0;
  cin >> s;
  n = s.size();
  cnt[0] = l[0] = r[0] = 0;
  for (int i = 1; i <= n; i++) {
    cnt[i] = 0;
    l[i] = r[i] = i;
    hsh[i] = (hsh[i - 1] * 727 + int(s[i - 1]));
  }
  iota(sufarr, sufarr + n, 1);
  sort(sufarr, sufarr + n, cmp);
  for (int i = 0; i + 1 < n; i++) {
    alive[n - sufarr[i] + 1] = i;
    int tmp = lcp(sufarr[i], sufarr[i + 1]);
    if (tmp) {
      vec[tmp].push_back(i);
    }
  }
  alive[n - sufarr[n - 1] + 1] = n - 1;
  for (int i = n; i > 0; i--) {
    cnt[alive[i]] = 1;
    res += 1;
    for (int j : vec[i]) {
      uni(j, j + 1);
    }
    vec[i].clear();
    ans += res;
  }
  cout << ans << "\n";
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int q;
  cin >> q;
  p[0] = 1;
  for (int i = 1; i < maxn; i++) {
    p[i] = (1ll * p[i - 1] * 727);
  }
  while (q--) {
    solve();
  }
  return 0;
}
