Now that you have proposed a fake post for the HC2 Facebook page, Heidi wants to measure the quality of the post before actually posting it. She recently came across a (possibly fake) article about the impact of fractal structure on multimedia messages and she is now trying to measure the self-similarity of the message, which is defined as

<image>

where the sum is over all nonempty strings p and <image> is the number of occurences of p in s as a substring. (Note that the sum is infinite, but it only has a finite number of nonzero summands.)

Heidi refuses to do anything else until she knows how to calculate this self-similarity. Could you please help her? (If you would like to instead convince Heidi that a finite string cannot be a fractal anyway – do not bother, we have already tried.)

Input

The input starts with a line indicating the number of test cases T (1 ≤ T ≤ 10). After that, T test cases follow, each of which consists of one line containing a string s (1 ≤ |s| ≤ 100 000) composed of lowercase letters (a-z).

Output

Output T lines, every line containing one number – the answer to the corresponding test case.

Example

Input

4
aa
abcd
ccc
abcc


Output

5
10
14
12

Note

A string s contains another string p as a substring if p is a contiguous subsequence of s. For example, ab is a substring of cab but not of acb.
#include <bits/stdc++.h>
using namespace std;
using ull = unsigned long long;
using ll = long long;
using ld = long double;
const int mod = 1e9 + 7;
const double pi = acos(-1.0);
const int inf = INT_MAX;
vector<int> suffixArray(string s) {
  s += char(0);
  int n = s.length();
  vector<int> sa(n);
  vector<int> c(n), cnt(max(256, n));
  for (int i = 0; i < n; i++) {
    cnt[s[i]]++;
  }
  for (int i = 1; i < 256; i++) {
    cnt[i] += cnt[i - 1];
  }
  for (int i = 0; i < n; i++) {
    sa[--cnt[s[i]]] = i;
  }
  c[sa[0]] = 0;
  int classes = 1;
  for (int i = 1; i < n; i++) {
    if (s[sa[i]] != s[sa[i - 1]]) classes++;
    c[sa[i]] = classes - 1;
  }
  vector<int> pn(n), cn(n);
  for (int h = 0; (1 << h) < n; h++) {
    for (int i = 0; i < n; i++) {
      pn[i] = (sa[i] + n - (1 << h)) % n;
    }
    fill(cnt.begin(), cnt.begin() + classes, 0);
    for (int i = 0; i < n; i++) {
      cnt[c[pn[i]]]++;
    }
    for (int i = 1; i < classes; i++) {
      cnt[i] += cnt[i - 1];
    }
    for (int i = n - 1; i >= 0; i--) {
      sa[--cnt[c[pn[i]]]] = pn[i];
    }
    cn[sa[0]] = 0;
    classes = 1;
    for (int i = 1; i < n; i++) {
      if (c[sa[i]] != c[sa[i - 1]] or
          c[(sa[i] + (1 << h)) % n] != c[(sa[i - 1] + (1 << h)) % n]) {
        classes++;
      }
      cn[sa[i]] = classes - 1;
    }
    c.swap(cn);
  }
  sa.erase(sa.begin());
  return sa;
}
vector<int> LCP(const string& s, const vector<int>& sa) {
  int n = s.length();
  vector<int> lcp(n - 1), rank(n);
  for (int i = 0; i < n; i++) {
    rank[sa[i]] = i;
  }
  int k = 0;
  for (int i = 0; i < n; i++) {
    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;
    if (k) {
      k--;
    }
  }
  return lcp;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int T;
  cin >> T;
  while (T--) {
    string s;
    cin >> s;
    int n = s.length();
    vector<int> sa = suffixArray(s);
    vector<int> lcp = LCP(s, sa);
    vector<int> l(n - 1, -1), r(n - 1, n - 1), st;
    for (int i = 0; i < n - 1; i++) {
      while (!st.empty() and lcp[st.back()] >= lcp[i]) {
        st.pop_back();
      }
      if (!st.empty()) {
        l[i] = st.back();
      }
      st.push_back(i);
    }
    st.clear();
    for (int i = n - 2; i >= 0; i--) {
      while (!st.empty() and lcp[st.back()] >= lcp[i]) {
        st.pop_back();
      }
      if (!st.empty()) {
        r[i] = st.back();
      }
      st.push_back(i);
    }
    ll ans = ((ll)n * n + n) / 2 - accumulate(lcp.begin(), lcp.end(), 0ll);
    set<pair<int, int>> rep;
    for (int i = 0; i < n - 1; i++) {
      if (!rep.count(make_pair(l[i], r[i]))) {
        int range = r[i] - l[i],
            mn = max(l[i] != -1 ? lcp[l[i]] : 0, r[i] != n - 1 ? lcp[r[i]] : 0);
        ans += (ll)range * range * (lcp[i] - mn) - (lcp[i] - mn);
        rep.emplace(l[i], r[i]);
      }
    }
    cout << ans << '\n';
  }
  return 0;
}
