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 std::vector;
const int N = 200010;
int _w;
int T, n;
char s[N];
namespace SAM {
int ch[N][26], nid;
int len[N], par[N], cnt[N];
vector<int> buc[N];
int append(int p, int c) {
  int np = nid++;
  len[np] = len[p] + 1;
  while (p != -1 && !ch[p][c]) ch[p][c] = np, p = par[p];
  if (p == -1) {
    par[np] = 0;
  } else {
    int q = ch[p][c];
    if (len[q] == len[p] + 1) {
      par[np] = q;
    } else {
      int nq = nid++;
      for (int i = 0; i < 26; ++i) ch[nq][i] = ch[q][i];
      par[nq] = par[q];
      len[nq] = len[p] + 1;
      par[q] = par[np] = nq;
      while (p != -1 && ch[p][c] == q) ch[p][c] = nq, p = par[p];
    }
  }
  return np;
}
void build(char *s) {
  memset(ch, 0, sizeof ch);
  nid = 1;
  len[0] = 0, par[0] = -1;
  int u = 0;
  for (; *s; ++s) u = append(u, *s - 'a');
}
void solve(char *s) {
  memset(cnt, 0, sizeof cnt);
  int u = 0;
  for (; *s; ++s) {
    u = ch[u][*s - 'a'];
    ++cnt[u];
  }
  for (int i = 0; i <= n; ++i) buc[i].clear();
  for (int i = 1; i < nid; ++i) buc[len[i]].push_back(i);
  for (int i = n; i; --i)
    for (int j = 0; j < (int)buc[i].size(); ++j) {
      int u = buc[i][j];
      cnt[par[u]] += cnt[u];
    }
  long long ans = 0;
  for (int i = 1; i < nid; ++i)
    ans += 1LL * cnt[i] * cnt[i] * (len[i] - len[par[i]]);
  printf("%lld\n", ans);
}
}  // namespace SAM
int main() {
  _w = scanf("%d", &T);
  while (T--) {
    _w = scanf("%s", s);
    n = (int)strlen(s);
    SAM::build(s);
    SAM::solve(s);
  }
  return 0;
}