#include <bits/stdc++.h>
#pragma GCC optimize("O2")
using namespace std;
const int maxn = 1e5 + 5;
const int mod = 1e9 + 7;
const int inf = 1e9;
const int del = 728729;
const int lg = 20;
long long poww(long long a, long long b, long long md) {
  return (!b ? 1
             : (b & 1 ? a * poww(a * a % md, b / 2, md) % md
                      : poww(a * a % md, b / 2, md) % md));
}
int pos[maxn][lg];
int P[maxn];
int nr[maxn];
int nl[maxn];
int lcp[maxn];
int pw;
int n;
string p;
stack<int> st;
bool cmp(int x, int y) {
  if (pos[x][pw - 1] != pos[y][pw - 1]) {
    return pos[x][pw - 1] < pos[y][pw - 1];
  }
  if (n < max(x, y) + (1 << (pw - 1))) {
    return y < x;
  }
  return pos[x + (1 << (pw - 1))][pw - 1] < pos[y + (1 << (pw - 1))][pw - 1];
}
void build() {
  for (int i = 1; i <= n; i++) {
    pos[i][0] = p[i - 1];
    P[i] = i;
  }
  for (pw = 1; pw < lg; pw++) {
    sort(P + 1, P + n + 1, cmp);
    pos[P[1]][pw] = 1;
    for (int i = 2; i <= n; i++) {
      pos[P[i]][pw] = pos[P[i - 1]][pw] + cmp(P[i - 1], P[i]);
    }
  }
}
int LCP(int x, int y) {
  int res = 0;
  if (y < x) {
    swap(x, y);
  }
  if (x == 0) {
    return 0;
  }
  for (int i = lg - 1; i >= 0; i--) {
    if (y + (1 << i) <= n + 1 && pos[x][i] == pos[y][i]) {
      x += 1 << i;
      y += 1 << i;
      res += 1 << i;
    }
  }
  return res;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> p;
    n = p.size();
    build();
    for (int i = 1; i < n; i++) {
      lcp[i] = LCP(P[i], P[i + 1]);
    }
    lcp[0] = -inf;
    lcp[n] = -inf;
    st.push(0);
    for (int i = 1; i < n; i++) {
      while (lcp[i] <= lcp[st.top()]) {
        st.pop();
      }
      nl[i] = st.top();
      st.push(i);
    }
    while (st.size()) {
      st.pop();
    }
    st.push(n);
    for (int i = n - 1; i; i--) {
      while (lcp[i] < lcp[st.top()]) {
        st.pop();
      }
      nr[i] = st.top();
      st.push(i);
    }
    long long ans = (long long)n * (n + 1) / 2;
    for (int i = 1; i < n; i++)
      ans += (long long)lcp[i] * (i - nl[i]) * (nr[i] - i);
    ans = (long long)2 * ans - (long long)n * (n + 1) / 2;
    cout << ans << "\n";
  }
  return 0;
}
