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;
char s[1000005];
int b[1000005];
struct sam_point {
  int fa, son[30];
  long long sum, len;
  void clear() {
    fa = len = sum = 0;
    for (int i = 0; i < 30; i++) son[i] = 0;
  }
};
struct SAM {
  int n, last, len, hea[1000005], nex[1000005], wen[1000005];
  sam_point a[1000005];
  long long ans;
  void clear() {
    for (int i = 1; i <= n; i++) a[i].clear(), hea[i] = 0;
    ans = len = 0;
    n = last = 1;
  }
  void add(int x, int y) {
    ++len;
    nex[len] = hea[x];
    wen[len] = y;
    hea[x] = len;
  }
  void insert(int x) {
    int p1 = last, p2 = ++n;
    last = n;
    a[p2].sum = 1;
    a[p2].len = a[p1].len + 1;
    for (; p1 && !a[p1].son[x]; p1 = a[p1].fa) a[p1].son[x] = p2;
    if (!p1)
      a[p2].fa = 1;
    else {
      int q1 = a[p1].son[x];
      if (a[p1].len + 1 == a[q1].len)
        a[p2].fa = q1;
      else {
        int q2 = ++n;
        a[q2] = a[q1];
        a[q2].sum = 0;
        a[q2].len = a[p1].len + 1;
        a[q1].fa = a[p2].fa = q2;
        for (; p1 && a[p1].son[x] == q1; p1 = a[p1].fa) a[p1].son[x] = q2;
      }
    }
  }
  void build() {
    for (int i = 2; i <= n; i++) add(a[i].fa, i);
  }
  void dfs(int x) {
    for (int i = hea[x]; i; i = nex[i]) {
      dfs(wen[i]);
      a[x].sum += a[wen[i]].sum;
    }
    ans += a[x].sum * a[x].sum * (a[x].len - a[a[x].fa].len);
  }
} sam;
int main() {
  int t;
  scanf("%d", &t);
  do {
    sam.clear();
    scanf("%s", s);
    int len = strlen(s);
    for (int i = 1; i <= len; i++) b[i] = 0;
    for (int i = 0; i <= len - 1; i++) sam.insert(s[i] - 97);
    sam.build();
    sam.dfs(1);
    printf("%lld\n", sam.ans);
    --t;
  } while (t);
}