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 mxl[200020], go[200020][26], siz[200020], par[200020], last = 1, ct = 1;
void extend(int ch) {
  int p = last, np = last = ++ct;
  siz[np] = 1, mxl[np] = mxl[p] + 1;
  while (p && !go[p][ch]) go[p][ch] = np, p = par[p];
  if (p) {
    int q = go[p][ch];
    if (mxl[q] == mxl[p] + 1)
      par[np] = q;
    else {
      int nq = ++ct;
      par[nq] = par[q], par[q] = par[np] = nq, mxl[nq] = mxl[p] + 1;
      for (register int i = 0; i <= 25; i++) go[nq][i] = go[q][i];
      while (p && go[p][ch] == q) go[p][ch] = nq, p = par[p];
    }
  } else
    par[np] = 1;
}
vector<int> g[200020];
void dfs(int x) {
  int si = g[x].size();
  for (register int i = 0; i < si; i++) {
    int op = g[x][i];
    dfs(op);
    siz[x] += siz[op];
  }
}
void update_parent_tree() {
  for (register int i = 1; i <= ct; i++) g[par[i]].push_back(i);
  dfs(1);
}
char s[200020];
int n;
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    long long int res = 0;
    scanf("%s", s + 1);
    n = strlen(s + 1);
    for (register int i = 1; i <= n; i++) extend(s[i] - 'a');
    update_parent_tree();
    for (register int i = 1; i <= ct; i++)
      res += 1ll * siz[i] * siz[i] * (mxl[i] - mxl[par[i]]);
    printf("%lld\n", res);
    for (register int i = 0; i <= ct; i++)
      for (register int j = 0; j <= 25; j++) go[i][j] = 0;
    for (register int i = 0; i <= ct; i++) siz[i] = par[i] = mxl[i] = 0;
    for (register int i = 0; i <= ct; i++) g[i].clear();
    last = ct = 1;
  }
  return 0;
}