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 long long maxn = 2e5 + 15;
inline long long read() {
  long long x = 0, f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = (x << 1) + (x << 3) + c - '0';
    c = getchar();
  }
  return x * f;
}
long long T, n, tot, last, len[maxn], size[maxn], link[maxn], nxt[maxn][30];
inline long long extend(long long last, long long x) {
  long long now = ++tot, pos = last, tmp, cln;
  len[now] = len[pos] + 1;
  size[now]++;
  while (pos != 0 && nxt[pos][x] == 0) nxt[pos][x] = now, pos = link[pos];
  if (!pos) {
    link[now] = 1;
    return now;
  }
  tmp = nxt[pos][x];
  if (len[tmp] == len[pos] + 1) {
    link[now] = tmp;
    return now;
  }
  cln = ++tot;
  len[cln] = len[pos] + 1;
  for (long long i = 1; i <= 26; i++) nxt[cln][i] = nxt[tmp][i];
  link[cln] = link[tmp];
  link[tmp] = link[now] = cln;
  while (pos != 0 && nxt[pos][x] == tmp) nxt[pos][x] = cln, pos = link[pos];
  return now;
}
string s;
long long beg[maxn], nex[maxn], to[maxn], e;
inline void add(long long x, long long y) {
  e++;
  nex[e] = beg[x];
  beg[x] = e;
  to[e] = y;
}
long long val[maxn], k;
inline void dfs(long long x) {
  for (long long i = beg[x]; i; i = nex[i]) dfs(to[i]), size[x] += size[to[i]];
  val[size[x]] += (len[x] - len[link[x]]);
}
inline void pre() {
  for (long long i = 0; i <= tot; i++) {
    val[i] = beg[i] = len[i] = size[i] = link[i] = 0;
    for (long long j = 1; j <= 26; j++) nxt[i][j] = 0;
  }
  e = tot = 0;
}
signed main() {
  T = read();
  while (T--) {
    pre();
    cin >> s;
    n = s.size();
    s = " " + s;
    last = ++tot;
    for (long long i = 1; i <= n; i++) last = extend(last, s[i] - 96);
    for (long long i = 2; i <= tot; i++) add(link[i], i);
    dfs(1);
    long long ans = 0;
    for (long long i = n; i >= 1; i--) ans += val[i] * i * i;
    printf("%lld\n", ans);
  }
  return 0;
}