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;
template <typename T>
void read(T &x) {
  x = 0;
  char ch = getchar();
  bool f = 0;
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = 1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9')
    x = (x << 1) + (x << 3) + ch - '0', ch = getchar();
  x = f ? -x : x;
}
const int N = 1e5 + 7;
int n, m;
char s[N];
struct SAM {
  const static int N = 2e5 + 7;
  int ch[N][26], len[N], fail[N], cnt[N], tot, lst;
  int id[N], ct[N];
  long long ans;
  void clean(int x) {
    fail[x] = 0;
    memset(ch[x], 0, sizeof(ch[x]));
  }
  void init() {
    tot = lst = 1;
    clean(1);
  }
  void ins(int c) {
    int p = lst, now = ++tot;
    len[now] = len[lst] + 1;
    cnt[now] = 1;
    lst = now;
    clean(now);
    while (p && !ch[p][c]) ch[p][c] = now, p = fail[p];
    if (!p)
      fail[now] = 1;
    else {
      int q = ch[p][c];
      if (len[q] == len[p] + 1)
        fail[now] = q;
      else {
        int clo = ++tot;
        clean(clo);
        len[clo] = len[p] + 1;
        cnt[clo] = 0;
        fail[clo] = fail[q];
        memcpy(ch[clo], ch[q], sizeof(ch[q]));
        while (p && ch[p][c] == q) ch[p][c] = clo, p = fail[p];
        fail[now] = fail[q] = clo;
      }
    }
  }
  void sol() {
    ans = 0;
    for (int i = (0), _ = (n); i <= _; ++i) ct[i] = 0;
    for (int i = (1), _ = (tot); i <= _; ++i) ++ct[len[i]];
    for (int i = (1), _ = (n); i <= _; ++i) ct[i] += ct[i - 1];
    for (int i = (1), _ = (tot); i <= _; ++i) id[ct[len[i]]--] = i;
    for (int j = (tot), _ = (1); j >= _; --j) {
      int i = id[j];
      ans += (long long)(len[i] - len[fail[i]]) * cnt[i] * cnt[i],
          cnt[fail[i]] += cnt[i];
    }
    printf("%lld", ans);
  }
} sam;
signed main() {
  int T;
  read(T);
  while (T--) {
    scanf("%s", s + 1);
    n = strlen(s + 1);
    sam.init();
    for (int i = (1), _ = (n); i <= _; ++i) sam.ins(s[i] - 'a');
    sam.sol();
    cout << endl;
  }
  return 0;
}