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 ch[200005][26], fail[200005], len[200005];
int ed[200005];
int rt, tot, last;
void reset(int i) {
  memset(ch[i], 0, sizeof(ch[i]));
  fail[i] = len[i] = ed[i] = 0;
}
void add(int x) {
  int p = last;
  int np = ++tot;
  reset(np);
  len[np] = len[p] + 1;
  ed[np] = 1;
  last = np;
  while (p && !ch[p][x]) {
    ch[p][x] = np;
    p = fail[p];
  }
  if (!p) {
    fail[np] = rt;
  } else {
    int q = ch[p][x];
    if (len[q] == len[p] + 1) {
      fail[np] = q;
    } else {
      int nq = ++tot;
      reset(nq);
      memcpy(ch[nq], ch[q], sizeof(ch[q]));
      len[nq] = len[p] + 1;
      fail[nq] = fail[q];
      fail[np] = fail[q] = nq;
      while (p && ch[p][x] == q) {
        ch[p][x] = nq;
        p = fail[p];
      }
    }
  }
}
void SuffixAutomaton(string &S) {
  rt = tot = last = 1;
  reset(1);
  int s = S.length();
  for (int i = (0); i <= (s - 1); i += (1)) {
    add(S[i] - 'a');
  }
}
int freq[100005], tpsort[200005];
void CountingSort(int s) {
  for (int i = (0); i <= (s); i += (1)) freq[i] = 0;
  for (int i = (1); i <= (tot); i += (1)) freq[len[i]]++;
  for (int i = (1); i <= (s); i += (1)) freq[i] += freq[i - 1];
  for (int i = (1); i <= (tot); i += (1)) tpsort[freq[len[i]]--] = i;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int t;
  cin >> t;
  for (int i = (1); i <= (t); i += (1)) {
    string S;
    cin >> S;
    int s = S.length();
    SuffixAutomaton(S);
    CountingSort(s);
    long long ans = 0;
    for (int i = (tot); i >= (1); i -= (1)) {
      int u = tpsort[i];
      ed[fail[u]] += ed[u];
      ans += 1LL * ed[u] * ed[u] * (len[u] - len[fail[u]]);
    }
    printf("%lld\n", ans);
  }
  return 0;
}