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 = 1e5 + 10;
const int N = 2e5 + 10;
const int M = 5e3 + 10;
const int inf = 0x3f3f3f3f;
const long long INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + 7;
const int mod2 = 998244353;
const int mod3 = 1e9 + 9;
const int hash1 = 131;
const int hash2 = 13331;
struct NODE {
  int ch[26];
  int len, fa, cnt;
};
struct SAM {
  int las = 1, tot = 1;
  NODE node[N << 1];
  void init() {
    memset(node, 0, sizeof(node));
    memset(c, 0, sizeof(c));
    las = 1, tot = 1;
  }
  void add(int c) {
    int p = las;
    int np = las = ++tot;
    node[np].len = node[p].len + 1;
    node[np].cnt = 1;
    for (; p && !node[p].ch[c]; p = node[p].fa) node[p].ch[c] = np;
    if (!p)
      node[np].fa = 1;
    else {
      int q = node[p].ch[c];
      if (node[q].len == node[p].len + 1)
        node[np].fa = q;
      else {
        int nq = ++tot;
        node[nq] = node[q];
        node[nq].len = node[p].len + 1;
        node[nq].cnt = 0;
        node[q].fa = node[np].fa = nq;
        for (; p && node[p].ch[c] == q; p = node[p].fa) node[p].ch[c] = nq;
      }
    }
  }
  int c[N << 1], a[N << 1];
  void toposort() {
    for (int i = 1; i <= tot; i++) c[node[i].len]++;
    for (int i = 1; i <= tot; i++) c[i] += c[i - 1];
    for (int i = 1; i <= tot; i++) a[c[node[i].len]--] = i;
    for (int i = tot; i; i--) {
      int x = a[i];
      int fa = node[x].fa;
      node[fa].cnt += node[x].cnt;
    }
  }
} sam;
int main() {
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    string str;
    cin >> str;
    sam.init();
    for (int i = 0; i < str.length(); i++) {
      sam.add(str[i] - 'a');
    }
    sam.toposort();
    long long ans = 0;
    for (int i = 1; i <= sam.tot; i++) {
      NODE x = sam.node[i];
      NODE fa = sam.node[x.fa];
      ans += (long long)(x.len - fa.len) * x.cnt * x.cnt;
    }
    cout << ans << endl;
  }
  return 0;
}