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;
int const MAXN = 2e5 + 10;
char s[MAXN];
int n, m;
struct SAM {
  int trans[MAXN << 1][26], link[MAXN << 1], len[MAXN << 1], siz[MAXN];
  int Cnt = 1;
  int insert(int c, int lst) {
    int np = ++Cnt, p = lst;
    len[np] = len[p] + 1;
    siz[np] = 1;
    for (; p && !trans[p][c]; p = link[p]) trans[p][c] = np;
    if (!p)
      link[np] = 1;
    else {
      int q = trans[p][c];
      if (len[q] == len[p] + 1)
        link[np] = q;
      else {
        int clone = ++Cnt;
        len[clone] = len[p] + 1;
        link[clone] = link[q];
        link[q] = link[np] = clone;
        memcpy(trans[clone], trans[q], sizeof(trans[q]));
        for (; p && trans[p][c] == q; p = link[p]) trans[p][c] = clone;
      }
    }
    return np;
  }
  int Rank[MAXN << 1], Bin[MAXN << 1];
  long long ans = 0;
  void run() {
    ans = 0;
    for (int i = 1; i <= Cnt; i++) Bin[len[i]]++;
    for (int i = 1; i <= Cnt; i++) Bin[i] += Bin[i - 1];
    for (int i = 1; i <= Cnt; i++) Rank[Bin[len[i]]--] = i;
    for (int i = Cnt; i >= 1; i--) {
      int p = Rank[i];
      siz[link[p]] += siz[p];
      ans += 1ll * siz[p] * siz[p] * (len[p] - len[link[p]]);
    }
    printf("%lld\n", ans);
  }
  void init() {
    memset(trans, 0, sizeof(trans));
    memset(link, 0, sizeof(link));
    memset(len, 0, sizeof(len));
    memset(siz, 0, sizeof(siz));
    Cnt = 1;
    ans = 0;
    memset(Rank, 0, sizeof(Rank));
    memset(Bin, 0, sizeof(Bin));
  }
} sam;
void work() {
  scanf("%s", s + 1);
  sam.init();
  int len = strlen(s + 1);
  for (int i = 1, lst = 1; i <= len; i++) {
    lst = sam.insert(s[i] - 'a', lst);
  }
  sam.run();
}
int main() {
  int T;
  scanf("%d", &T);
  for (int i = 1; i <= T; i++) {
    work();
  }
  return 0;
}