#include <bits/stdc++.h>
using namespace std;
const int MN = 101234;
int gap, n, rnk[MN], sa[MN], diff[MN], lcp[MN];
string s;
bool Cmp(int a, int b) {
  if (rnk[a] != rnk[b]) return rnk[a] < rnk[b];
  return (a + gap < n && b + gap < n) ? rnk[a + gap] < rnk[b + gap] : a > b;
}
void build() {
  for (int i = 0; i < n; i++) rnk[i] = s[i], sa[i] = i;
  for (gap = 1;; gap *= 2) {
    sort(sa, sa + n, Cmp);
    for (int i = 1; i < n; i++) diff[i] = diff[i - 1] + Cmp(sa[i - 1], sa[i]);
    rnk[sa[0]] = 0;
    for (int i = 1; i < n; i++) rnk[sa[i]] = diff[i];
    if (rnk[sa[n - 1]] == n - 1) return;
  }
}
void LCP() {
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (rnk[i] == n - 1) {
      ans = 0;
      continue;
    }
    int r = rnk[i];
    for (int j = sa[r + 1]; s[i + ans] == s[j + ans];) ans++;
    lcp[r] = ans;
    if (ans) ans--;
  }
}
int main() {
  int q;
  cin >> q;
  while (q--) {
    cin >> s;
    n = s.size();
    s += "$";
    build();
    LCP();
    vector<pair<int, int> > vec;
    long long sum = 0, ans = 2 * n - 1;
    for (int i = 0; i < n - 1; i++) {
      int x = lcp[i], cnt = 1;
      while (vec.size() && vec.back().first > x) {
        sum -= 1ll * vec.back().first * vec.back().second;
        cnt += vec.back().second;
        vec.pop_back();
      }
      vec.push_back({x, cnt});
      sum += 1ll * vec.back().first * vec.back().second;
      ans += 2 * sum + i;
    }
    cout << ans << endl;
  }
}
