#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
inline void smax(int &x, int y) { x = max(x, y); }
inline void smin(int &x, int y) { x = min(x, y); }
struct suffix {
  ll ind;
  ll 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<ll> &suffixarr) {
  ll 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<ll> ind(n);
  for (ll k = 4; k < 2 * n; k = k * 2) {
    ll rank = 0;
    ll 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++) {
      ll 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;
  }
}
vector<int> kasai(string s, vector<ll> sa) {
  int n = s.size(), k = 0;
  vector<int> lcp(n, 0);
  vector<int> rank(n, 0);
  for (int i = 0; i < n; i++) rank[sa[i]] = i;
  for (int i = 0; i < n; i++, k ? k-- : 0) {
    if (rank[i] == n - 1) {
      k = 0;
      continue;
    }
    int j = sa[rank[i] + 1];
    while (i + k < n && j + k < n && s[i + k] == s[j + k]) k++;
    lcp[rank[i]] = k;
  }
  return lcp;
}
int lft[100010];
int rgt[100010];
int main() {
  int tcase;
  cin >> tcase;
  for (__typeof(tcase + 1) tc = (1) - ((1) > (tcase + 1));
       tc != (tcase + 1) - ((1) > (tcase + 1));
       tc += 1 - 2 * ((1) > (tcase + 1))) {
    string st;
    cin >> st;
    int n = st.size();
    vector<ll> sa(n);
    calsuffixarray(st, sa);
    vector<int> tmp = kasai(st, sa);
    vector<ll> lcp;
    lcp.push_back(0);
    for (int i = 0; i < tmp.size(); i++) lcp.push_back(tmp[i]);
    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);
    }
    cout << ans << "\n";
  }
}
