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;
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 << 3) + (x << 1) + c - '0';
    c = getchar();
  }
  return x * F;
}
char s[200000 + 5], t[200000 + 5];
int T, n, Q, mx[200000 + 5], siz[200000 + 5];
long long ans;
vector<int> G[200000 + 5];
void add(int x) { G[mx[x]].push_back(x); }
struct suffix_automation {
  int ch[200000 + 5][26], fa[200000 + 5];
  int ed, cnt, rt;
  void init() {
    memset(ch, 0, sizeof(ch)), memset(fa, 0, sizeof(fa)),
        memset(siz, 0, sizeof(siz)), memset(mx, 0, sizeof(mx));
    ed = rt = cnt = 1;
    for (int i = 1; i <= n; i++) G[i].clear();
  }
  void Insert(int c) {
    int cur = ++cnt, p = ed;
    mx[cur] = mx[ed] + 1, siz[cur] = 1;
    ed = cur;
    add(cur);
    while (p && ch[p][c] == 0) ch[p][c] = cur, p = fa[p];
    if (!p)
      fa[cur] = 1;
    else {
      int q = ch[p][c];
      if (mx[q] == mx[p] + 1)
        fa[cur] = q;
      else {
        int np = ++cnt;
        for (int i = 0; i < 26; i++) ch[np][i] = ch[q][i];
        fa[np] = fa[q], mx[np] = mx[p] + 1;
        fa[cur] = fa[q] = np;
        add(np);
        while (p && ch[p][c] == q) ch[p][c] = np, p = fa[p];
      }
    }
  }
} sam;
int main() {
  T = read();
  while (T--) {
    ans = 0;
    scanf("%s", s + 1);
    n = strlen(s + 1);
    sam.init();
    for (int i = 1; i <= n; i++) sam.Insert(s[i] - 'a');
    for (int i = n; i >= 1; i--)
      for (int j = 0; j < G[i].size(); j++) {
        int v = G[i][j];
        if (sam.fa[v]) siz[sam.fa[v]] += siz[v];
        ans += 1LL * (mx[v] - mx[sam.fa[v]]) * siz[v] * siz[v];
      }
    printf("%lld\n", ans);
  }
}
