#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); }
const int MAXN = 1000005;
int str[MAXN];
int _rank[MAXN], pos[MAXN];
int cnt[MAXN], _next[MAXN];
bool bh[MAXN], b2h[MAXN];
bool smaller_first_char(int a, int b) { return str[a] < str[b]; }
void suffixSort(int n) {
  for (int i = 0; i < n; ++i) {
    pos[i] = i;
  }
  sort(pos, pos + n, smaller_first_char);
  for (int i = 0; i < n; ++i) {
    bh[i] = i == 0 || str[pos[i]] != str[pos[i - 1]];
    b2h[i] = false;
  }
  for (int h = 1; h < n; h <<= 1) {
    int buckets = 0;
    for (int i = 0, j; i < n; i = j) {
      j = i + 1;
      while (j < n && !bh[j]) j++;
      _next[i] = j;
      buckets++;
    }
    if (buckets == n) break;
    for (int i = 0; i < n; i = _next[i]) {
      cnt[i] = 0;
      for (int j = i; j < _next[i]; ++j) {
        _rank[pos[j]] = i;
      }
    }
    cnt[_rank[n - h]]++;
    b2h[_rank[n - h]] = true;
    for (int i = 0; i < n; i = _next[i]) {
      for (int j = i; j < _next[i]; ++j) {
        int s = pos[j] - h;
        if (s >= 0) {
          int head = _rank[s];
          _rank[s] = head + cnt[head]++;
          b2h[_rank[s]] = true;
        }
      }
      for (int j = i; j < _next[i]; ++j) {
        int s = pos[j] - h;
        if (s >= 0 && b2h[_rank[s]]) {
          for (int k = _rank[s] + 1; !bh[k] && b2h[k]; k++) b2h[k] = false;
        }
      }
    }
    for (int i = 0; i < n; ++i) {
      pos[_rank[i]] = i;
      bh[i] |= b2h[i];
    }
  }
  for (int i = 0; i < n; ++i) {
    _rank[pos[i]] = i;
  }
}
int lcp[MAXN];
void getHeight(int n) {
  for (int i = 0; i < n; ++i) _rank[pos[i]] = i;
  lcp[0] = 0;
  for (int i = 0, h = 0; i < n; ++i) {
    if (_rank[i] > 0) {
      int j = pos[_rank[i] - 1];
      while (i + h < n && j + h < n && str[i + h] == str[j + h]) h++;
      lcp[_rank[i]] = h;
      if (h > 0) h--;
    }
  }
}
int lft[MAXN];
int rgt[MAXN];
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();
    for (int i = 0; i < n; i++) str[i] = st[i] - '0';
    suffixSort(n);
    getHeight(n);
    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";
  }
}
