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 = 2e5;
const long long M = 1e4;
const long long inf = 2e18;
inline long long read() {
  long long s = 0;
  register bool neg = 0;
  register char c = getchar();
  for (; c < '0' || c > '9'; c = getchar()) neg |= (c == '-');
  for (; c >= '0' && c <= '9'; s = s * 10 + (c ^ 48), c = getchar())
    ;
  s = (neg ? -s : s);
  return s;
}
long long t, a, tmp[N + 5], sa[N + 5], rk[N + 5], h[N + 5], cnt[N + 5], p, q,
    L[N + 5], sk[N + 5], r, _h[N + 5], re[N + 5];
char s[N + 5];
void init() {
  p = 127;
  for (long long i = (1); i <= (p); ++i) cnt[i] = 0;
  for (long long i = (1); i <= (a); ++i) cnt[rk[i] = s[i]]++;
  for (long long i = (1); i <= (p); ++i) cnt[i] += cnt[i - 1];
  for (long long i = (1); i <= (a); ++i) sa[cnt[rk[i]]--] = i;
  for (long long i = 1;; i <<= 1, p = q) {
    q = 0;
    for (long long j = (a - i + 1); j <= (a); ++j) tmp[++q] = j;
    for (long long j = (1); j <= (a); ++j)
      if (sa[j] > i) tmp[++q] = sa[j] - i;
    for (long long j = (1); j <= (p); ++j) cnt[j] = 0;
    for (long long j = (1); j <= (a); ++j) cnt[rk[j]]++;
    for (long long j = (1); j <= (p); ++j) cnt[j] += cnt[j - 1];
    for (long long j = (a); j >= (1); --j)
      sa[cnt[rk[tmp[j]]]--] = tmp[j], tmp[j] = 0;
    swap(tmp, rk);
    q = 1;
    rk[sa[1]] = 1;
    for (long long j = (2); j <= (a); ++j)
      rk[sa[j]] =
          (tmp[sa[j]] == tmp[sa[j - 1]] && tmp[sa[j] + i] == tmp[sa[j - 1] + i])
              ? q
              : ++q;
    if (q == a) break;
  }
  for (long long i = (1); i <= (a); ++i) sa[rk[i]] = i;
  p = 0;
  for (long long j = (1); j <= (a); ++j) {
    if (p) --p;
    while (s[j + p] == s[sa[rk[j] - 1] + p]) ++p;
    h[j] = p;
  }
  for (long long i = (1); i <= (a); ++i) _h[rk[i]] = h[i];
}
signed main() {
  t = read();
  while (t--) {
    memset(L, 0, sizeof(L));
    scanf("%s", s + 1);
    a = strlen(s + 1);
    init();
    long long ans = 0, o = 0, lst = 0;
    r = 0;
    for (long long i = (1); i <= (a); ++i) {
      re[i] = i;
      while (r && _h[sk[r]] > _h[i]) {
        o += (i - re[L[sk[r]]]) * (_h[sk[r]] - max(_h[i], _h[L[sk[r]]]));
        ans += (i - re[L[sk[r]]]) * (i - re[L[sk[r]]]) *
               (_h[sk[r]] - max(_h[i], _h[L[sk[r]]]));
        r--;
      }
      if (r && _h[sk[r]] == _h[i]) {
        re[sk[r]] = i;
        continue;
      }
      if (r)
        L[i] = sk[r];
      else
        L[i] = lst;
      if (_h[i])
        sk[++r] = i;
      else
        lst = i;
    }
    while (r) {
      ans += (a - re[L[sk[r]]] + 1) * (a - re[L[sk[r]]] + 1) *
             (_h[sk[r]] - _h[sk[r - 1]]);
      o += (a - re[L[sk[r]]] + 1) * (_h[sk[r]] - _h[sk[r - 1]]);
      r--;
    }
    ans += a * (a + 1) / 2 - o;
    printf("%lld\n", ans);
  }
  return 0;
}