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;
char s[100005];
int n, gap, t;
int sa[100005], pos[100005], tmp[100005], lcp[100005], cum[100005];
bool sufCmp(int i, int j) {
  if (pos[i] != pos[j]) return pos[i] < pos[j];
  i += gap;
  j += gap;
  return (i < n && j < n) ? pos[i] < pos[j] : i > j;
}
void buildSA() {
  for (int i = 0; i < (int)(n); ++i) sa[i] = i, pos[i] = s[i];
  for (gap = 1;; gap *= 2) {
    sort(sa, sa + n, sufCmp);
    for (int i = 0; i < (int)(n - 1); ++i)
      tmp[i + 1] = tmp[i] + sufCmp(sa[i], sa[i + 1]);
    for (int i = 0; i < (int)(n); ++i) pos[sa[i]] = tmp[i];
    if (tmp[n - 1] == n - 1) break;
  }
}
void buildLCP() {
  for (int i = 0, k = 0; i < n; ++i)
    if (pos[i] != n - 1) {
      for (int j = sa[pos[i] + 1]; s[i + k] == s[j + k];) ++k;
      lcp[pos[i]] = k;
      if (k) --k;
    }
}
long long st[100005][18];
void Init() {
  long long sz = n;
  for (int i = 0; i < sz; i++) st[i][0] = lcp[i];
  for (int k = 1; k < 18; k++) {
    for (int i = 0; i <= sz - (1 << k); i++)
      st[i][k] = min(st[i][k - 1], st[i + (1 << (k - 1))][k - 1]);
  }
}
long long query(long long l, long long r) {
  long long k = log(r - l + 1) / log(2);
  return min(st[l][k], st[r - (1 << k) + 1][k]);
}
pair<int, int> bs(int pos, int val) {
  if (lcp[pos] <= val) return {pos - 1, n - sa[pos]};
  int b = pos, e = n;
  while (b + 1 < e) {
    int mid = (b + e) / 2;
    if (query(pos, mid) <= val)
      e = mid;
    else
      b = mid;
  }
  return {b, query(pos, b)};
}
int main() {
  scanf("%d", &t);
  while (t--) {
    scanf("%s", s);
    n = strlen(s);
    buildSA();
    buildLCP();
    Init();
    long long ans = 0;
    for (int i = 0; i < n; i++) {
      int cur = i ? lcp[i - 1] : 0;
      while (1) {
        pair<int, int> x = bs(i, cur);
        int pos = x.first, nxt = x.second;
        long long m = 1LL * (pos - i + 2) * (pos - i + 2);
        ans += 1LL * (nxt - cur) * m;
        if (pos < i)
          break;
        else
          cur = nxt;
      }
    }
    printf("%lld\n", ans);
  }
  return 0;
}