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>
template <typename T>
void read(T& x) {
  x = 0;
  char ch = getchar();
  long long f = 1;
  while (!isdigit(ch)) {
    if (ch == '-') f *= -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = x * 10 + ch - 48;
    ch = getchar();
  }
  x *= f;
}
using namespace std;
const int N = 100010;
struct p {
  int x, v;
};
char s[N];
int sa[N], rk[N << 1], oldrk[N << 1], cnt[N], id[N], tmp[N];
int height[N];
p st[N];
bool cmp(int, int, int);
void suf_arr(char*, int*, int*, int*, int n, int m);
int main() {
  int t;
  int n;
  int tail;
  long long ans;
  scanf("%d", &t);
  while (t--) {
    scanf("%s", s + 1);
    n = strlen(s + 1);
    suf_arr(s, sa, rk, height, n, 300);
    height[n + 1] = 0;
    tail = 0;
    ans = 1LL * n * (n + 1) / 2;
    for (int i = 1; i <= n + 1; ++i) {
      int tmp = height[i], lst = i;
      while (tail && st[tail - 1].v > tmp) {
        int xl = st[tail - 1].x - 1, xr = i - 1;
        int vl = max(st[tail - 2].v, tmp) + 1, vr = st[tail - 1].v;
        ans += 1LL * (vr - vl + 1) * (xr - xl) * (xr - xl + 1);
        lst = st[tail - 1].x;
        --tail;
      }
      if (tail && st[tail - 1].v == tmp) continue;
      st[tail++] = {lst, tmp};
    }
    printf("%lld\n", ans);
  }
  return 0;
}
bool cmp(int x, int y, int w) {
  return oldrk[x] == oldrk[y] && oldrk[x + w] == oldrk[y + w];
}
void suf_arr(char* s, int* sa, int* rk, int* height, int n, int m) {
  int p;
  memset(cnt, 0, (m + 1) * sizeof(int));
  memset(oldrk, 0, (2 * n + 1) * sizeof(int));
  for (int i = 1; i <= n; ++i) ++cnt[rk[i] = s[i]];
  for (int i = 1; i <= m; ++i) cnt[i] += cnt[i - 1];
  for (int i = 1; i <= n; ++i) sa[cnt[rk[i]]--] = i;
  for (int w = 1, p = 0; w < n; w <<= 1, m = p, p = 0) {
    for (int i = n - w + 1; i <= n; ++i) id[++p] = i;
    for (int i = 1; i <= n; ++i) {
      if (sa[i] > w) id[++p] = sa[i] - w;
    }
    memset(cnt, 0, (m + 1) * sizeof(int));
    for (int i = 1; i <= n; ++i) ++cnt[tmp[i] = rk[id[i]]];
    for (int i = 1; i <= m; ++i) cnt[i] += cnt[i - 1];
    for (int i = n; i; --i) sa[cnt[tmp[i]]--] = id[i];
    memcpy(oldrk, rk, (n + 1) * sizeof(int));
    p = 0;
    for (int i = 1; i <= n; ++i) rk[sa[i]] = cmp(sa[i], sa[i - 1], w) ? p : ++p;
  }
  for (int i = 1, k = 0; i <= n; ++i) {
    if (k) --k;
    while (s[i + k] == s[sa[rk[i] - 1] + k]) ++k;
    height[rk[i]] = k;
  }
}