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 + 5;
struct SAMNode {
  int len, fa, ch[27];
  void init() { memset(ch, 0, sizeof(ch)), len = 0, fa = 0; }
} nd[maxn];
int lst, nd_cnt, cnt[maxn];
inline void SAM_init() { nd_cnt = lst = 1, nd[1].init(); }
void SAM_insert(int c) {
  int p = lst, np = lst = ++nd_cnt;
  nd[np].init();
  cnt[np] = 1;
  nd[np].len = nd[p].len + 1;
  for (; p && !nd[p].ch[c]; p = nd[p].fa) nd[p].ch[c] = np;
  if (!p)
    nd[np].fa = 1;
  else {
    int v = nd[p].ch[c];
    if (nd[v].len == nd[p].len + 1)
      nd[np].fa = v;
    else {
      int nv = ++nd_cnt;
      cnt[nv] = 0;
      nd[nv] = nd[v], nd[nv].len = nd[p].len + 1;
      nd[v].fa = nd[np].fa = nv;
      for (; p && nd[p].ch[c] == v; p = nd[p].fa) nd[p].ch[c] = nv;
    }
  }
}
struct Edge {
  int v, nex;
  Edge(int v = 0, int nex = 0) : v(v), nex(nex) {}
} E[maxn << 1];
int hd[maxn], tote;
void G_addedge(int u, int v) {
  E[++tote] = Edge(v, hd[u]), hd[u] = tote;
  E[++tote] = Edge(u, hd[v]), hd[v] = tote;
}
void G_build() {
  tote = 0, memset(hd, 0, sizeof(hd));
  for (int i = 2; i <= nd_cnt; i++) G_addedge(nd[i].fa, i);
}
long long ans;
void G_dfs(int u, int fa) {
  for (int i = hd[u]; i; i = E[i].nex) {
    int v = E[i].v;
    if (v == fa) continue;
    G_dfs(v, u), cnt[u] += cnt[v];
  }
  ans += 1ll * (nd[u].len - nd[fa].len) * cnt[u] * cnt[u];
}
string str;
void solve() {
  ans = 0, SAM_init();
  cin >> str;
  for (int i = 0; i < str.size(); i++) SAM_insert(str[i] - 'a');
  G_build(), G_dfs(1, 0);
  printf("%lld\n", ans);
}
int main() {
  int T;
  scanf("%d", &T);
  while (T--) solve();
  return 0;
}