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;
inline long long read() {
  long long x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + (ch ^ 48);
    ch = getchar();
  }
  return x * f;
}
const long long N = 200010;
struct edge {
  long long v, nxt;
} e[N];
long long head[N], cnt, n, len[N], fail[N], nxt[N][26], tail, tot, siz[N];
char s[N];
inline void add(long long u, long long v) {
  e[++cnt].v = v;
  e[cnt].nxt = head[u];
  head[u] = cnt;
}
inline void SAMinit() {
  memset(len, 0, sizeof(len));
  memset(fail, 0, sizeof(fail));
  memset(nxt, 0, sizeof(nxt));
  memset(head, cnt = 0, sizeof(head));
  memset(siz, 0, sizeof(siz));
  fail[0] = -1;
  tail = tot = 0;
}
inline void SAMadd(long long c) {
  len[++tot] = len[tail] + 1;
  long long p = tail;
  tail = tot;
  for (; p != -1 && !nxt[p][c]; p = fail[p]) nxt[p][c] = tail;
  if (p == -1) {
    fail[tail] = 0;
    return;
  }
  long long q = nxt[p][c];
  if (len[q] == len[p] + 1) {
    fail[tail] = q;
    return;
  }
  len[++tot] = len[p] + 1;
  fail[tot] = fail[q];
  for (long long i = 0; i <= 25; ++i) nxt[tot][i] = nxt[q][i];
  for (; p != -1 && nxt[p][c] == q; p = fail[p]) nxt[p][c] = tot;
  fail[tail] = fail[q] = tot;
}
inline void dfs(long long u) {
  for (long long i = head[u]; i; i = e[i].nxt) {
    long long v = e[i].v;
    dfs(v);
    siz[u] += siz[v];
  }
}
inline void mian() {
  scanf("%s", s + 1);
  n = strlen(s + 1);
  SAMinit();
  long long ans = 0;
  for (long long i = 1; i <= n; ++i) SAMadd(s[i] - 'a');
  long long p = 0;
  for (long long i = 1; i <= n; ++i) {
    p = nxt[p][s[i] - 'a'];
    siz[p] = 1;
  }
  for (long long i = 1; i <= tot; ++i) add(fail[i], i);
  dfs(0);
  for (long long i = 1; i <= tot; ++i)
    ans += siz[i] * siz[i] * (len[i] - len[fail[i]]);
  printf("%lld\n", ans);
}
signed main() {
  long long qwq = read();
  while (qwq--) mian();
  return 0;
}
