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;
const long long N = 300 * 1000 + 20, MOD = 1e9 + 7, base = 727;
long long hsh[N], n, pw[N], a[N], ca[N], L[N], R[N], sum, tot, tmp, _lcp[N];
string s, b;
bool add[N];
vector<long long> vec[N];
inline long long get_hash(long long l, long long r) {
  return hsh[r] - hsh[l] * pw[r - l];
}
inline long long lcp(long long i, long long j) {
  long long l = 0, r = n - max(i, j) + 1;
  while (l + 1 < r) {
    long long mid = (l + r) / 2;
    if (get_hash(i, i + mid) == get_hash(j, j + mid))
      l = mid;
    else
      r = mid;
  }
  return l;
}
inline bool cmp(long long i, long long j) {
  long long l = lcp(i, j);
  if (l + max(i, j) == n)
    return i > j;
  else
    return s[i + l] < s[j + l];
}
inline void _union(long long i) {
  long long x = L[i], y = L[i + 1];
  sum -= ca[x] * ca[x] + ca[y] * ca[y];
  tmp -= ca[x] + ca[y];
  if (ca[x] == 0) ca[x] = 1;
  if (ca[y] == 0) ca[y] = 1;
  ca[L[i]] += ca[L[i + 1]];
  ca[R[i + 1]] = ca[L[i]];
  R[L[i]] = R[i + 1];
  L[R[i + 1]] = L[i];
  sum += ca[x] * ca[x];
  tmp += ca[x];
  return;
}
inline void solve() {
  cin >> s;
  n = s.size();
  long long M = n + 1;
  iota(a, a + M, 0);
  iota(L, L + M, 0);
  iota(R, R + M, 0);
  fill(ca, ca + M, 0);
  for (long long i = 0; i < n; i++) {
    hsh[i + 1] = hsh[i] * base + s[i];
  }
  sort(a, a + n, cmp);
  for (long long i = 0; i < n - 1; i++) _lcp[i] = lcp(a[i], a[i + 1]);
  {
    stack<long long> s;
    s.push(-1);
    tot = sum = 0;
    long long al = (n * (n + 1)) / 2;
    for (long long i = 0; i < n - 1; i++) {
      while (s.top() != -1 && _lcp[s.top()] >= _lcp[i]) {
        long long id = s.top();
        s.pop();
        sum -= _lcp[id] * (id - s.top());
      }
      sum += _lcp[i] * (i - s.top());
      tot += sum;
      s.push(i);
    }
    tot *= 2;
    cout << tot + al << '\n';
  }
  return;
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(nullptr);
  cout.tie(nullptr);
  ;
  for (long long i = pw[0] = 1; i < N; i++) pw[i] = pw[i - 1] * base;
  long long q;
  cin >> q;
  while (q--) solve();
  return 0;
}