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>
template <typename T>
void read(T& x) {
  x = 0;
  int flag = 1;
  char c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-') flag = -1;
  for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
  x *= flag;
}
using namespace std;
const int maxn = 200010;
int n;
char s[maxn];
struct SAM {
  int fa[maxn], len[maxn], ch[maxn][26], sz[maxn], cnt, lst;
  bool vis[maxn];
  vector<int> e[maxn];
  void clear() {
    for (int i = 0; i <= (int)2 * n + 1; i++) {
      fa[i] = -1, len[i] = sz[i] = vis[i] = 0;
      memset(ch[i], 0, sizeof(ch[i]));
      e[i].clear();
    }
    cnt = lst = 0;
  }
  void ins(int c) {
    int p = lst, u = ++cnt;
    len[u] = len[p] + 1, sz[u] = 1;
    for (; ~p && !ch[p][c]; p = fa[p]) ch[p][c] = u;
    if (p == -1)
      fa[u] = 0;
    else {
      int q = ch[p][c];
      if (len[p] + 1 == len[q])
        fa[u] = q;
      else {
        int nq = ++cnt;
        memcpy(ch[nq], ch[q], sizeof(ch[nq]));
        fa[nq] = fa[q], len[nq] = len[p] + 1;
        fa[u] = fa[q] = nq;
        for (; ~p && ch[p][c] == q; p = fa[p]) ch[p][c] = nq;
      }
    }
    lst = u;
  }
  long long res;
  void dfs(int u) {
    vis[u] = 1;
    for (int i = 0; i <= (int)e[u].size() - 1; i++) {
      if (!vis[e[u][i]]) dfs(e[u][i]);
      sz[u] += sz[e[u][i]];
    }
    int tmp = len[u] - len[fa[u]];
    res += 1ll * sz[u] * sz[u] * tmp;
  }
  void solve() {
    res = 0;
    for (int i = 1; i <= (int)cnt; i++) e[fa[i]].push_back(i);
    for (int i = 0; i <= (int)cnt; i++)
      if (!vis[i]) dfs(i);
    printf("%lld\n", res);
  }
} qwq;
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    scanf("%s", s + 1);
    n = strlen(s + 1);
    qwq.clear();
    for (int i = 1; i <= (int)n; i++) qwq.ins(s[i] - 'a');
    qwq.solve();
  }
  return 0;
}