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 S = 400004;
int last, tr[S][26], fail[S], len[S], tot = 0, eg = 1, h[S], nx[S << 1],
                                      v[S << 1], e[S];
char c[S];
void insert(int &last, char c) {
  c -= 'a';
  int p = last, now = ++tot;
  len[now] = len[last] + 1;
  while (p != -1 && !tr[p][c]) {
    tr[p][c] = now;
    p = fail[p];
  }
  if (p == -1)
    fail[now] = 0;
  else {
    int q = tr[p][c];
    if (len[p] + 1 == len[q])
      fail[now] = q;
    else {
      int w = ++tot;
      len[w] = len[p] + 1;
      fail[w] = fail[q];
      for (int i = 0; i < 26; ++i) tr[w][i] = tr[q][i];
      fail[q] = fail[now] = w;
      while (p != -1 && tr[p][c] == q) {
        tr[p][c] = w;
        p = fail[p];
      }
    }
  }
  last = now;
}
inline void egadd(int uu, int vv) {
  nx[++eg] = h[uu];
  h[uu] = eg;
  v[eg] = vv;
}
long long res = 0;
void dfs(int x) {
  for (int i = h[x]; i; i = nx[i]) {
    dfs(v[i]);
    e[x] += e[v[i]];
  }
  res += (len[x] - len[fail[x]]) * 1ll * e[x] * e[x];
}
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    tot = last = 0;
    fail[0] = -1;
    res = 0;
    scanf("%s", c + 1);
    for (int i = 1; c[i]; ++i) insert(last, c[i]);
    int p = 0;
    for (int i = 1; c[i]; ++i) {
      p = tr[p][c[i] - 'a'];
      e[p] = 1;
    }
    for (int i = 1; i <= tot; ++i) egadd(fail[i], i);
    dfs(0);
    printf("%lld\n", res);
    for (int i = 0; i <= tot; ++i) {
      fail[i] = len[i] = 0;
      h[i] = 0;
      e[i] = 0;
      for (int j = 0; j < 26; ++j) tr[i][j] = 0;
    }
    eg = 1;
  }
  return 0;
}