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;
template <typename T>
inline void Read(T &n) {
  char ch;
  bool flag = false;
  while (!isdigit(ch = getchar()))
    if (ch == '-') flag = true;
  for (n = ch ^ 48; isdigit(ch = getchar());
       n = (n << 1) + (n << 3) + (ch ^ 48))
    ;
  if (flag) n = -n;
}
const int MAXN = 100005;
struct node {
  int son[26], fa, len, sz;
} t[MAXN << 1];
int node_cnt = 1;
int prv = 1;
inline void insert(int v) {
  int x = ++node_cnt;
  t[x].len = t[prv].len + 1;
  t[x].sz = 1;
  while (prv and !t[prv].son[v]) t[prv].son[v] = x, prv = t[prv].fa;
  if (!prv)
    t[x].fa = 1;
  else {
    int p = t[prv].son[v];
    if (t[p].len == t[prv].len + 1)
      t[x].fa = p;
    else {
      int new_p = ++node_cnt;
      t[new_p].len = t[prv].len + 1;
      t[new_p].fa = t[p].fa;
      t[p].fa = t[x].fa = new_p;
      copy(t[p].son, t[p].son + 26, t[new_p].son);
      while (prv and t[prv].son[v] == p) t[prv].son[v] = new_p, prv = t[prv].fa;
    }
  }
  prv = x;
}
char s[MAXN];
int S[MAXN], id[MAXN << 1];
int main() {
  int T;
  Read(T);
  while (T--) {
    scanf("%s", s + 1);
    int n = strlen(s + 1);
    prv = node_cnt = 1;
    memset(t, 0, sizeof t);
    memset(S, 0, sizeof S);
    for (register int i = 1; i <= n; i++) insert(s[i] - 'a');
    for (register int i = 1; i <= node_cnt; i++) S[t[i].len]++;
    for (register int i = 1; i <= n; i++) S[i] += S[i - 1];
    for (register int i = 1; i <= node_cnt; i++) id[S[t[i].len]--] = i;
    for (register int i = node_cnt; i; i--) t[t[id[i]].fa].sz += t[id[i]].sz;
    long long ans = 0;
    for (register int i = 2; i <= node_cnt; i++)
      ans += 1ll * t[i].sz * t[i].sz * (t[i].len - t[t[i].fa].len);
    printf("%lld\n", ans);
  }
  return 0;
}