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;
inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (!(ch >= '0' && ch <= '9')) {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + (ch - '0');
    ch = getchar();
  }
  return x * f;
}
void chkmin(int &a, int b) {
  if (b < a) a = b;
}
void chkmax(int &a, int b) {
  if (b > a) a = b;
}
const int N = 200005;
char s[N];
int n, sa[N], rk[N], height[N], t1[N], t2[N], c[N];
int ans, lim;
inline bool cmp(int *y, int a, int b, int l) {
  return y[a] == y[b] &&
         ((a + l <= n) ? y[a + l] : -1) == ((b + l <= n) ? y[b + l] : -1);
}
inline void SA() {
  int p, m = 500;
  int *x = t1, *y = t2;
  for (int i = 1; i <= m; i++) c[i] = 0;
  for (int i = 1; i <= n; i++) c[x[i] = s[i - 1]]++;
  for (int i = 2; i <= m; i++) c[i] += c[i - 1];
  for (int i = n; i >= 1; i--) sa[c[x[i]]--] = i;
  for (int k = 1; k <= n; k <<= 1) {
    p = 0;
    for (int i = n - k + 1; i <= n; i++) y[++p] = i;
    for (int i = 1; i <= n; i++)
      if (sa[i] > k) y[++p] = sa[i] - k;
    for (int i = 1; i <= m; i++) c[i] = 0;
    for (int i = 1; i <= n; i++) c[x[i]]++;
    for (int i = 2; i <= m; i++) c[i] += c[i - 1];
    for (int i = n; i >= 1; i--) sa[c[x[y[i]]]--] = y[i];
    swap(x, y);
    p = 0;
    x[sa[1]] = ++p;
    for (int i = 2; i <= n; i++) {
      if (!cmp(y, sa[i], sa[i - 1], k))
        x[sa[i]] = ++p;
      else
        x[sa[i]] = p;
    }
    m = p;
    if (m == n) break;
  }
}
inline void HEI() {
  for (int i = 1; i <= n; i++) rk[sa[i]] = i;
  int k = 0;
  for (int i = 1; i <= n; i++) {
    if (k > 0) k--;
    if (rk[i] == 1) continue;
    int j = sa[rk[i] - 1];
    while (i + k - 1 < n && j + k - 1 < n && s[i + k - 1] == s[j + k - 1]) k++;
    height[rk[i]] = k;
  }
}
int top = 0, stk[N][2];
int main() {
  int T = read();
  while (T--) {
    scanf("%s", s);
    n = strlen(s);
    SA();
    HEI();
    long long ans = 0;
    top = 0;
    long long delta = 0;
    stk[0][0] = 0;
    stk[0][1] = 0;
    for (int i = 1; i <= n; i++) {
      while (top && height[i] <= stk[top][0])
        delta -= (long long)(stk[top][1] - stk[top - 1][1]) * stk[top][0],
            top--;
      stk[++top][0] = height[i];
      stk[top][1] = i - 1;
      delta += (long long)(stk[top][1] - stk[top - 1][1]) * height[i];
      ans += delta + (n - sa[i] + 1);
    }
    top = 0;
    delta = 0;
    stk[0][0] = 0;
    stk[0][1] = n + 1;
    for (int i = n; i >= 1; i--) {
      while (top && height[i + 1] <= stk[top][0])
        delta -= (long long)(stk[top - 1][1] - stk[top][1]) * stk[top][0],
            top--;
      stk[++top][0] = height[i + 1];
      stk[top][1] = i + 1;
      delta += (long long)(stk[top - 1][1] - stk[top][1]) * height[i + 1];
      ans += delta;
    }
    printf("%lld\n", ans);
  }
  return 0;
}