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;
const int maxn = 2e5 + 10;
char s[maxn];
int n;
long long ans;
namespace SAM {
int fa[maxn], ch[maxn][26], mxlen[maxn], cend[maxn], cnode, root, lst;
vector<int> g[maxn];
vector<int> len[maxn];
inline int getnewnode() {
  fa[cnode] = -1;
  memset(ch[cnode], -1, sizeof(ch[0]));
  cend[cnode] = 0;
  return cnode++;
}
void init(int m) {
  for (int i = 0; i <= m; i++) len[i].clear();
  cnode = 0;
  lst = root = getnewnode();
  mxlen[root] = 0;
}
void extend(int c) {
  int cur = getnewnode();
  mxlen[cur] = mxlen[lst] + 1;
  len[mxlen[cur]].push_back(cur);
  cend[cur]++;
  int p;
  for (p = lst; p != -1 && ch[p][c] == -1; p = fa[p]) ch[p][c] = cur;
  lst = cur;
  if (p == -1) {
    fa[cur] = root;
    return;
  }
  int q = ch[p][c];
  if (mxlen[q] == mxlen[p] + 1) {
    fa[cur] = q;
    return;
  }
  int clone = getnewnode();
  mxlen[clone] = mxlen[p] + 1;
  len[mxlen[clone]].push_back(clone);
  fa[clone] = fa[q];
  for (int i = 0; i < 26; i++) ch[clone][i] = ch[q][i];
  fa[q] = fa[cur] = clone;
  for (; p != -1 && ch[p][c] == q; p = fa[p]) ch[p][c] = clone;
}
void build() {
  for (int i = 0; i < n; i++) extend(s[i] - 'a');
  for (int l = n; l >= 1; l--) {
    for (int i = 0; i < len[l].size(); i++) {
      int x = len[l][i];
      int cnt = mxlen[x] - mxlen[fa[x]];
      cend[fa[x]] += cend[x];
      ans += cnt * 1ll * cend[x] * 1ll * cend[x];
    }
  }
}
}  // namespace SAM
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    scanf("%s", s);
    n = strlen(s);
    ans = 0;
    SAM::init(n);
    SAM::build();
    printf("%I64d\n", ans);
  }
  return 0;
}