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 MAXN = 1e5;
struct Suffix_Automaton {
  int rt, siz, last;
  int trs[2 * MAXN + 5][26], fail[2 * MAXN + 5];
  int mxlen[2 * MAXN + 5], endpos[2 * MAXN + 5];
  Suffix_Automaton() {
    rt = siz = last = 1;
    memset(trs, 0, sizeof(trs));
    memset(fail, 0, sizeof(fail));
    memset(mxlen, 0, sizeof(mxlen));
    memset(endpos, 0, sizeof(endpos));
    memset(tp, 0, sizeof(tp));
  }
  void Extend(int chr) {
    int nw = ++siz, u = last;
    mxlen[nw] = mxlen[u] + 1;
    for (; u && !trs[u][chr]; u = fail[u]) trs[u][chr] = nw;
    if (!u)
      fail[nw] = 1;
    else {
      int v = trs[u][chr];
      if (mxlen[v] == mxlen[u] + 1)
        fail[nw] = v;
      else {
        int w = ++siz;
        mxlen[w] = mxlen[u] + 1;
        fail[w] = fail[v];
        for (int i = 0; i < 26; i++) trs[w][i] = trs[v][i];
        fail[v] = fail[nw] = w;
        for (; u && trs[u][chr] == v; u = fail[u]) trs[u][chr] = w;
      }
    }
    last = nw;
    endpos[last]++;
  }
  void Build(int len, char *str) {
    for (int i = 1; i <= len; i++) Extend(str[i] - 'a');
  }
  int buc[MAXN + 5], tp[2 * MAXN + 5];
  void Sort() {
    for (int i = 0; i <= MAXN; i++) buc[i] = 0;
    for (int i = 1; i <= siz; i++) buc[mxlen[i]]++;
    for (int i = 1; i <= MAXN; i++) buc[i] += buc[i - 1];
    for (int i = siz; i >= 1; i--) tp[buc[mxlen[i]]--] = i;
  }
  long long Calc() {
    Sort();
    long long Ans = 0;
    for (int i = siz; i >= 2; i--) {
      int u = tp[i];
      Ans += 1ll * (mxlen[u] - mxlen[fail[u]]) * endpos[u] * endpos[u];
      endpos[fail[u]] += endpos[u];
    }
    return Ans;
  }
} SAM;
int T, n;
char str[MAXN + 5];
int main() {
  scanf("%d\n", &T);
  while (T--) {
    scanf("%s", str + 1);
    n = strlen(str + 1);
    SAM = Suffix_Automaton();
    SAM.Build(n, str);
    printf("%lld\n", SAM.Calc());
  }
  return 0;
}