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>
const int mod = 998244353;
using namespace std;
inline int read() {
  int f = 1, x = 0;
  char s = getchar();
  while (!isdigit(s)) {
    if (s == '-') f = -1;
    s = getchar();
  }
  while (isdigit(s)) {
    x = (x << 1) + (x << 3) + (s ^ 48), s = getchar();
  }
  return x * f;
}
int t, strl;
long long ans;
char str[2010000];
struct Suffix_Tree {
  int ch[2010000][27], link[2010000], len[2010000], start[2010000], s[2010000],
      rem, tot, siz[2010000], n, now;
  inline void reset(void) {
    memset(ch[tot], 0, sizeof(ch[tot]));
    while (tot--) memset(ch[tot], 0, sizeof(ch[tot]));
    n = rem = 0, tot = 1, len[0] = 0x3f3f3f3f, now = 1;
  }
  inline int New(int st, int le) {
    link[++tot] = 1, start[tot] = st, len[tot] = le;
    return tot;
  }
  inline void extend(int x) {
    s[++n] = x, rem++;
    for (int last = 1; rem;) {
      while (rem > len[ch[now][s[n - rem + 1]]])
        rem -= len[now = ch[now][s[n - rem + 1]]];
      int &v = ch[now][s[n - rem + 1]], c = s[start[v] + rem - 1];
      if (!v || x == c) {
        link[last] = now, last = now;
        if (!v)
          v = New(n - rem + 1, 0x3f3f3f3f);
        else
          break;
      } else {
        int u = New(start[v], rem - 1);
        ch[u][c] = v, ch[u][x] = New(n, 0x3f3f3f3f);
        start[v] += rem - 1, len[v] -= rem - 1;
        link[last] = v = u, last = u;
      }
      if (now == 1)
        rem--;
      else
        now = link[now];
    }
  }
  inline void adjust(int x) {
    int cnt = 0;
    siz[x] = 0;
    for (int i = 0; i <= 26; i++)
      if (ch[x][i]) ++cnt, adjust(ch[x][i]), siz[x] += siz[ch[x][i]];
    if (!cnt) len[x] = strl - start[x] + 1, siz[x] = 1;
  }
  inline void dfs(int x, int dep) {
    int cnt = 0;
    ans += 1ll * siz[x] * siz[x] * len[x];
    for (int i = 0; i <= 26; i++)
      if (ch[x][i]) ++cnt, dfs(ch[x][i], dep + len[ch[x][i]]);
  }
} tree;
signed main(void) {
  t = read();
  while (t--) {
    scanf("%s", str + 1);
    strl = strlen(str + 1);
    tree.reset(), ans = 0;
    for (int i = 1; i <= strl; i++) tree.extend(str[i] - 'a');
    tree.extend(26), tree.adjust(1);
    tree.dfs(1, 0), printf("%lld\n", ans);
  }
  return 0;
}