Problem: 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.

Solution (in CPP):

Solution:
#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";
  }
}