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;
void getint(int &v) {
  char ch, fu = 0;
  for (ch = '*'; (ch < '0' || ch > '9') && ch != '-'; ch = getchar())
    ;
  if (ch == '-') fu = 1, ch = getchar();
  for (v = 0; ch >= '0' && ch <= '9'; ch = getchar()) v = v * 10 + ch - '0';
  if (fu) v = -v;
}
long long ans;
const int INF = 1e9 + 10;
int n, m, t[1000010], T, rk[1000010], top, stk[1000010], h[1000010], l[1000010],
    r[1000010], sa[1000010], tt[1000010];
char s[1000010];
void da() {
  for (int i = 0; i <= m; ++i) t[i] = 0;
  for (int i = 1; i <= n; ++i) t[s[i] - 'a' + 1]++;
  for (int i = 1; i <= m; ++i) t[i] += t[i - 1];
  for (int i = 1; i <= n; ++i) sa[t[s[i] - 'a' + 1]--] = i;
  for (int i = 1; i <= n; ++i)
    rk[sa[i]] = rk[sa[i - 1]] + (s[sa[i]] != s[sa[i - 1]]);
  for (int j = 1; j < n; j <<= 1) {
    int p = 0;
    for (int i = n - j + 1; i <= n; ++i) tt[++p] = i;
    for (int i = 1; i <= n; ++i)
      if (sa[i] > j) tt[++p] = sa[i] - j;
    for (int i = 0; i <= n; ++i) t[i] = 0;
    for (int i = 1; i <= n; ++i) t[rk[i]]++;
    for (int i = 1; i <= n; ++i) t[i] += t[i - 1];
    for (int i = n; i >= 1; --i) sa[t[rk[tt[i]]]--] = tt[i];
    for (int i = 1; i <= n; ++i) {
      int A = sa[i - 1], B = sa[i];
      tt[B] = tt[A] + (rk[A] != rk[B] || rk[A + j] != rk[B + j]);
    }
    for (int i = 1; i <= n; ++i) rk[i] = tt[i];
  }
}
void ht() {
  for (int i = 1; i <= n; ++i) {
    int A = rk[i], B = rk[i - 1], p = sa[rk[i] - 1];
    int t = max(h[B] - 1, 0);
    while (s[i + t] == s[p + t]) t++;
    h[A] = t;
  }
}
int main() {
  scanf("%d", &T);
  while (T--) {
    scanf("%s", s + 1);
    n = strlen(s + 1);
    m = 26;
    da();
    ht();
    top = 0;
    stk[++top] = 0;
    h[0] = -INF;
    for (int i = 1; i <= n; ++i) {
      while (top > 0 && h[i] < h[stk[top]]) top--;
      l[i] = i - stk[top] - 1;
      stk[++top] = i;
    }
    top = 0;
    stk[++top] = n + 1;
    h[n + 1] = -INF;
    for (int i = n; i >= 1; --i) {
      while (top > 0 && h[i] <= h[stk[top]]) top--;
      r[i] = stk[top] - 1 - i;
      stk[++top] = i;
    }
    ans = (long long)n * (n + 1) / 2;
    for (int i = 1; i <= n; ++i)
      ans += (long long)h[i] * (r[i] + 1) * (l[i] + 1) * 2;
    cout << ans << endl;
  }
  return 0;
}