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;
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;
char s[maxn];
int nxt[maxn][26], par[maxn], len[maxn], num[maxn], rk[maxn], tp[maxn];
int n, cnt, last;
int add(int l) {
  ++cnt;
  memset(nxt[cnt], 0, sizeof nxt[cnt]);
  len[cnt] = l, num[cnt] = 0;
  return cnt;
}
void init() {
  cnt = 0;
  last = add(0);
}
void insert(char ch) {
  int t = ch - 'a', p = last, cur;
  cur = last = add(len[p] + 1);
  while (p && !nxt[p][t]) nxt[p][t] = cur, p = par[p];
  if (!p) {
    par[cur] = 1;
    return;
  }
  int q = nxt[p][t];
  if (len[q] == len[p] + 1) {
    par[cur] = q;
    return;
  }
  int nq = add(len[p] + 1);
  memcpy(nxt[nq], nxt[q], sizeof nxt[q]);
  par[nq] = par[q], par[q] = par[cur] = nq;
  while (p && nxt[p][t] == q) nxt[p][t] = nq, p = par[p];
}
void rsort() {
  for (int i = 0; i <= cnt; ++i) tp[i] = 0;
  for (int i = 1; i <= cnt; ++i) ++tp[len[i]];
  for (int i = 1; i <= cnt; ++i) tp[i] += tp[i - 1];
  for (int i = cnt; i >= 1; --i) rk[tp[len[i]]--] = i;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    init();
    cin >> s + 1;
    n = strlen(s + 1);
    for (int i = 1; i <= n; ++i) {
      insert(s[i]);
      num[last] = 1;
    }
    rsort();
    long long ret = 0;
    for (int i = cnt; i >= 2; --i) {
      int u = rk[i];
      num[par[u]] += num[u];
      ret += num[u] * 1ll * num[u] * (len[u] - len[par[u]]);
    }
    cout << ret << "\n";
  }
  return 0;
}