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.
#include <bits/stdc++.h>
using namespace std;
char str[200000 + 5];
long long f[200000 + 5];
struct node {
  int nxt[26];
  int fa;
  long long len;
} t[200000 + 10];
long long ans = 0;
int last, tot;
vector<int> g[200000 + 5];
void insrt(int x) {
  int nd = ++tot;
  f[nd] = 1;
  int p = last;
  last = nd;
  t[nd].len = t[p].len + 1;
  for (; p && t[p].nxt[x] == 0; p = t[p].fa) t[p].nxt[x] = nd;
  if (!p)
    t[nd].fa = 1;
  else {
    int q = t[p].nxt[x];
    if (t[p].len + 1 == t[q].len)
      t[nd].fa = q;
    else {
      int nt = ++tot;
      t[nt] = t[q];
      t[nd].fa = t[q].fa = nt;
      t[nt].len = t[p].len + 1;
      for (; t[p].nxt[x] == q && p; p = t[p].fa) t[p].nxt[x] = nt;
    }
  }
}
void init() {
  last = 1, tot = 1;
  memset(t, 0, sizeof(t));
  for (int i = 0; i <= 200000 + 3; i++) g[i].clear(), f[i] = 0;
}
void dfs(int u) {
  for (int i = 0; i < g[u].size(); i++) {
    dfs(g[u][i]);
    f[u] += f[g[u][i]];
  }
  ans += (t[u].len - t[t[u].fa].len) * f[u] * f[u];
  return;
}
int main() {
  int T_T;
  cin >> T_T;
  while (T_T--) {
    scanf("%s", str);
    int len = strlen(str);
    init();
    ans = 0;
    for (int i = 0; i < len; i++) insrt(str[i] - 'a');
    for (int i = 1; i <= tot; i++) g[t[i].fa].push_back(i);
    dfs(0);
    printf("%lld\n", ans);
  }
  return 0;
}
