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.
#include <bits/stdc++.h>
inline long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
inline void sub(long long &a, long long b) {
  a -= b;
  if (a < 0) a += 1000000009;
}
inline void add(long long &a, long long b) {
  a += b;
  if (a >= 1000000009) a -= 1000000009;
}
template <typename T>
inline T const &MAX(T const &a, T const &b) {
  return a > b ? a : b;
}
template <typename T>
inline T const &MIN(T const &a, T const &b) {
  return a < b ? a : b;
}
inline long long qp(long long a, long long b) {
  long long ans = 1;
  while (b) {
    if (b & 1) ans = ans * a % 1000000009;
    a = a * a % 1000000009, b >>= 1;
  }
  return ans;
}
inline long long qp(long long a, long long b, long long c) {
  long long ans = 1;
  while (b) {
    if (b & 1) ans = ans * a % c;
    a = a * a % c, b >>= 1;
  }
  return ans;
}
using namespace std;
const unsigned long long ba = 233;
const double eps = 1e-5;
const long long INF = 0x3f3f3f3f3f3f3f3f;
const int N = 100000 + 10, maxn = 1000000 + 10, inf = 0x3f3f3f3f;
char s[N];
struct SAM {
  int last, cnt;
  int ch[N << 1][26], fa[N << 1], l[N << 1], sz[N << 1];
  int a[N << 1], c[N << 1];
  void ins(int c) {
    int p = last, np = ++cnt;
    last = np;
    l[np] = l[p] + 1;
    for (; p && !ch[p][c]; p = fa[p]) ch[p][c] = np;
    if (!p)
      fa[np] = 1;
    else {
      int q = ch[p][c];
      if (l[p] + 1 == l[q])
        fa[np] = q;
      else {
        int nq = ++cnt;
        l[nq] = l[p] + 1;
        memcpy(ch[nq], ch[q], sizeof(ch[q]));
        fa[nq] = fa[q];
        fa[q] = fa[np] = nq;
        for (; ch[p][c] == q; p = fa[p]) ch[p][c] = nq;
      }
    }
    sz[np] = 1;
  }
  void topo() {
    for (int i = 1; i <= cnt; i++) c[l[i]]++;
    for (int i = 1; i <= cnt; i++) c[i] += c[i - 1];
    for (int i = 1; i <= cnt; i++) a[c[l[i]]--] = i;
  }
  void build() {
    memset(ch, 0, sizeof ch);
    memset(sz, 0, sizeof sz);
    memset(fa, 0, sizeof fa);
    memset(l, 0, sizeof l);
    memset(a, 0, sizeof a);
    memset(c, 0, sizeof c);
    last = cnt = 1;
    for (int i = 0; s[i]; i++) ins(s[i] - 'a');
    topo();
    for (int i = cnt; i; i--) sz[fa[a[i]]] += sz[a[i]];
    long long ans = 0;
    for (int i = 2; i <= cnt; i++)
      ans += 1ll * (l[i] - l[fa[i]]) * sz[i] * sz[i];
    printf("%lld\n", ans);
  }
} sam;
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    scanf("%s", s);
    sam.build();
  }
  return 0;
}
