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 EE = 1200000;
char a[EE];
struct tree {
  int fa, len;
  int ch[30];
  tree() {
    memset(ch, 0, sizeof(ch));
    len = fa = 0;
  }
} tree[EE];
int top = 1, last = 1;
int fir[EE], nxt[EE << 1], to[EE << 1], ztop = 0;
long long aans[EE];
long long ans = 0;
void add(int qap) {
  int p = last, np = last = ++top;
  aans[top] = 1;
  tree[np].len = tree[p].len + 1;
  while (p && !tree[p].ch[qap]) tree[p].ch[qap] = np, p = tree[p].fa;
  if (!p) {
    tree[np].fa = 1;
    return;
  }
  int q = tree[p].ch[qap];
  if (tree[q].len == tree[p].len + 1) {
    tree[np].fa = q;
    return;
  }
  int nq = ++top;
  tree[nq] = tree[q];
  tree[nq].len = tree[p].len + 1;
  tree[q].fa = tree[np].fa = nq;
  while (p && tree[p].ch[qap] == q) tree[p].ch[qap] = nq, p = tree[p].fa;
}
int maxx(int x, int y) { return x > y ? x : y; }
void addition(int x, int y) {
  nxt[++ztop] = fir[x], fir[x] = ztop, to[ztop] = y;
}
void dfs(int x, int fa) {
  for (int i = fir[x]; i; i = nxt[i]) {
    dfs(to[i], x);
    aans[x] += aans[to[i]];
  }
  ans += aans[x] * aans[x] * (tree[x].len - tree[fa].len);
}
void csh() {
  memset(fir, 0, sizeof(fir));
  memset(nxt, 0, sizeof(nxt));
  memset(to, 0, sizeof(to));
  memset(tree, 0, sizeof(tree));
  memset(aans, 0, sizeof(aans));
  memset(a, 0, sizeof(a));
  top = last = 1;
  ztop = ans = 0;
}
void sol() {
  csh();
  scanf("%s", a);
  int llen = strlen(a);
  int zz = 0;
  while (zz < llen) add(a[zz++] - 'a' + 1);
  for (int i = 2; i <= top; i++) addition(tree[i].fa, i);
  dfs(1, 1);
  printf("%lld\n", ans);
}
signed main() {
  int T;
  cin >> T;
  while (T) {
    T--;
    sol();
  }
}