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 N = 1e5 + 100;
struct graph {
  int head[N << 1], nxt[N << 1], to[N << 1], sz;
  void init() { memset(head, 0, sizeof(head)), sz = 0; }
  graph() { init(); }
  void push(int a, int b) { nxt[++sz] = head[a], to[sz] = b, head[a] = sz; }
  int &operator[](const int a) { return to[a]; }
} g;
char str[N];
long long answer;
struct Suffix_Automata {
  int maxlen[N << 1], trans[N << 1][26], link[N << 1], Size, Last;
  int endcnt[N << 1];
  void init() { Size = Last = 1; }
  void clean(int whi_node) {
    memset(trans[whi_node], 0, sizeof(trans[whi_node]));
    link[whi_node] = 0;
  }
  inline void Extend(int id) {
    int cur = (++Size), p;
    clean(cur);
    maxlen[cur] = maxlen[Last] + 1;
    endcnt[cur] = 1;
    for (p = Last; p && !trans[p][id]; p = link[p]) trans[p][id] = cur;
    if (!p)
      link[cur] = 1;
    else {
      int q = trans[p][id];
      if (maxlen[q] == maxlen[p] + 1)
        link[cur] = q;
      else {
        int clone = (++Size);
        endcnt[clone] = 0;
        maxlen[clone] = maxlen[p] + 1;
        memcpy(trans[clone], trans[q], sizeof(trans[q]));
        link[clone] = link[q];
        for (; p && trans[p][id] == q; p = link[p]) trans[p][id] = clone;
        link[cur] = link[q] = clone;
      }
    }
    Last = cur;
  }
  void dfs(int u) {
    for (int i = g.head[u]; i; i = g.nxt[i]) {
      dfs(g[i]);
      endcnt[u] += endcnt[g[i]];
    }
    if (u != 1)
      answer += 1ll * endcnt[u] * endcnt[u] * (maxlen[u] - maxlen[link[u]]);
    maxlen[u] = 0;
  }
  void suf_slink_tree() {
    g.init();
    for (int i = 2; i <= Size; i++) {
      g.push(link[i], i);
    }
    dfs(1);
    printf("%lld\n", answer);
  }
} T;
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    T.init();
    g.init();
    T.clean(1);
    answer = 0;
    scanf("%s", str + 1);
    int len = strlen(str + 1);
    for (int i = 1; i <= len; i++) T.Extend(str[i] - 'a');
    T.suf_slink_tree();
  }
  return 0;
}