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 N = 1e5 + 7;
struct SAM {
  struct node {
    int son[26], pre, len;
  } a[N << 1];
  int f[N << 1];
  vector<int> e[N << 1];
  int tot, tail = 0;
  void init() {
    for (int i = int(0); i <= int(tot); ++i) {
      e[i].clear(), f[i] = 0;
      memset(a[i].son, 0, sizeof(a[i].son));
      a[i].pre = a[i].len = 0;
    }
    tot = tail = 0;
  }
  void ins(int k) {
    int p, q, np = ++tot, nq;
    a[np].len = a[tail].len + 1;
    f[np] = 1;
    for (p = tail; p && !a[p].son[k]; p = a[p].pre) a[p].son[k] = np;
    if (!a[p].son[k]) {
      a[p].son[k] = np;
      a[np].pre = p;
    } else {
      q = a[p].son[k];
      if (a[q].len == a[p].len + 1)
        a[np].pre = q;
      else {
        a[nq = ++tot] = a[q];
        a[nq].len = a[p].len + 1;
        a[q].pre = a[np].pre = nq;
        for (; a[p].son[k] == q; p = a[p].pre) a[p].son[k] = nq;
      }
    }
    tail = np;
  }
  void dfs(int u) {
    for (int v : e[u]) dfs(v);
    f[a[u].pre] += f[u];
  }
  void solve() {
    for (int i = int(1); i <= int(tot); ++i) e[a[i].pre].push_back(i);
    dfs(0);
    long long ans = 0;
    for (int i = int(1); i <= int(tot); ++i) {
      ans += 1ll * (a[i].len - a[a[i].pre].len) * f[i] * f[i];
    }
    printf("%lld\n", ans);
  }
} sam;
char s[N];
int main() {
  int _;
  scanf("%d", &_);
  while (_--) {
    sam.init();
    scanf("%s", s);
    int len = strlen(s);
    for (int i = int(0); i <= int(len - 1); ++i) sam.ins(s[i] - 'a');
    sam.solve();
  }
}