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 = 2e5 + 10, size = 1 << 20;
template <class o>
void qr(o &x) {
  char c = getchar();
  x = 0;
  int f = 1;
  while (!isdigit(c)) {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (isdigit(c)) x = x * 10 + c - '0', c = getchar();
  x *= f;
}
template <class o>
void qw(o x) {
  if (x / 10) qw(x / 10);
  putchar(x % 10 + '0');
}
template <class o>
void pr1(o x) {
  if (x < 0) x = -x, putchar('-');
  qw(x);
  putchar(' ');
}
template <class o>
void pr2(o x) {
  if (x < 0) x = -x, putchar('-');
  qw(x);
  puts("");
}
int T, n, last, tot, f[N], b[N], c[N];
char s[N];
long long ans;
struct node {
  int fa, len, v[26];
} tr[N];
void add(int c) {
  int p = last, x = last = ++tot;
  tr[x].len = tr[p].len + 1;
  for (; p && tr[p].v[c] == 0; p = tr[p].fa) tr[p].v[c] = x;
  if (!p)
    tr[x].fa = 1;
  else {
    int q = tr[p].v[c], y;
    if (tr[p].len + 1 == tr[q].len)
      tr[x].fa = q;
    else {
      tr[y = ++tot] = tr[q];
      tr[y].len = tr[p].len + 1;
      tr[q].fa = tr[x].fa = y;
      for (; p && tr[p].v[c] == q; p = tr[p].fa) tr[p].v[c] = y;
    }
  }
}
void init() {
  memset(tr + 1, 0, tot * 28 << 2);
  memset(f + 1, 0, tot << 2);
  memset(c, 0, n + 1 << 2);
  last = tot = 1;
  ans = n = 0;
}
int main() {
  qr(T);
  while (T--) {
    init();
    scanf("%s", s + 1);
    for (int i = 1; s[i]; i++) n++, add(s[i] -= 'a'), f[last]++;
    for (int i = 1; i <= tot; i++) c[tr[i].len]++;
    for (int i = 1; i <= n; i++) c[i] += c[i - 1];
    for (int i = tot; i; i--) b[c[tr[i].len]--] = i;
    for (int i = tot, j; i; i--) {
      j = b[i];
      f[tr[j].fa] += f[j];
      ans += (long long)f[j] * f[j] * (tr[j].len - tr[tr[j].fa].len);
    }
    pr2(ans);
  }
  return 0;
}