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 int N = 4e5 + 11;
namespace SAM {
char S[N];
long long ans = 0;
int ch[N][27], fa[N], l[N], sz[N];
int las = 1, cnt = 1, n, q;
vector<int> to[N];
inline void extend(int c) {
  int p = las, np = ++cnt;
  las = cnt, l[np] = l[p] + 1, sz[np] = 1;
  for (; p && !ch[p][c]; p = fa[p]) ch[p][c] = np;
  if (!p)
    fa[np] = 1;
  else {
    int q = ch[p][c];
    if (l[q] == l[p] + 1)
      fa[np] = q;
    else {
      int nq = ++cnt;
      l[nq] = l[p] + 1;
      memcpy(ch[nq], ch[q], sizeof ch[q]);
      fa[nq] = fa[q], fa[q] = fa[np] = nq;
      for (; p && ch[p][c] == q; p = fa[p]) ch[p][c] = nq;
    }
  }
}
inline void dfs(int x) {
  for (auto v : to[x]) {
    dfs(v);
    sz[x] += sz[v];
  }
  ans += 1ll * sz[x] * sz[x] * (l[x] - l[fa[x]]);
}
inline void main() {
  ans = 0;
  las = 1, cnt = 1;
  scanf("%s", S + 1);
  n = strlen(S + 1);
  for (register int i = 1; i <= n; ++i) extend(S[i] - 'a');
  for (register int i = 2; i <= cnt; ++i) to[fa[i]].push_back(i);
  dfs(1);
  printf("%lld\n", ans);
  for (register int i = 1; i <= cnt; ++i)
    to[i].clear(), memset(ch[i], 0, sizeof ch[i]), sz[i] = l[i] = fa[i] = 0;
}
}  // namespace SAM
int main() {
  int T;
  for (scanf("%d", &T); T--;) SAM::main();
  return 0;
}