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.
#include <bits/stdc++.h>
using namespace std;
const long long maxn = 1e5 + 5;
struct SAMnode {
  long long len, f, s[30];
};
SAMnode t[maxn << 1];
long long las = 1, tot = 1;
void ins(char cc) {
  long long c = cc - 'a';
  long long p = las, np = ++tot;
  las = np;
  t[np].len = t[p].len + 1;
  for (; p && !t[p].s[c]; p = t[p].f) t[p].s[c] = np;
  if (!p) {
    t[np].f = 1;
  } else {
    long long q = t[p].s[c];
    if (t[q].len == t[p].len + 1) {
      t[np].f = q;
    } else {
      long long nq = ++tot;
      t[nq] = t[q];
      t[nq].len = t[p].len + 1;
      for (; p && t[p].s[c] == q; p = t[p].f) t[p].s[c] = nq;
      t[np].f = t[q].f = nq;
    }
  }
  return;
}
long long sz[maxn << 1];
vector<long long> adj[maxn << 1];
void build() {
  for (int i = 2; i <= tot; i++) {
    adj[t[i].f].push_back(i);
  }
  return;
}
long long ans = 0;
void dfs(long long now) {
  for (int i = 0; i < adj[now].size(); i++) {
    long long t = adj[now][i];
    dfs(t);
    sz[now] += sz[t];
  }
  ans += sz[now] * sz[now] * (t[now].len - t[t[now].f].len);
  return;
}
long long T, n;
char s[maxn];
int main() {
  scanf("%lld", &T);
  while (T--) {
    scanf("%s", s + 1);
    n = strlen(s + 1);
    for (int i = 1; i <= tot; i++) {
      memset(t[i].s, 0, sizeof(t[i].s));
      t[i].len = t[i].f = 0;
      adj[i].clear();
      sz[i] = 0;
    }
    las = tot = 1;
    for (int i = 1; i <= n; i++) {
      ins(s[i]);
    }
    long long now = 1;
    memset(sz, 0, sizeof(sz));
    for (int i = 1; i <= n; i++) {
      now = t[now].s[s[i] - 'a'];
      sz[now]++;
    }
    build();
    ans = 0;
    dfs(1);
    printf("%lld\n", ans);
  }
  return 0;
}
