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>
int dr[] = {2, 2, -2, -2, 1, -1, 1, -1};
int dc[] = {1, -1, 1, -1, 2, 2, -2, -2};
int dr1[] = {0, 0, 0, 1, 1, 1, -1, -1, -1};
int dc1[] = {-1, 0, 1, -1, 0, 1, -1, 0, 1};
int dr2[] = {0, 0, 1, -1};
int dc2[] = {1, -1, 0, 0};
using namespace std;
long long int siz, last, occur[200005];
struct automata {
  long long int len, link, next[26];
} st[200005];
void inser(char c) {
  long long int now = ++siz;
  st[now].len = st[last].len + 1;
  long long int p, q, cl;
  occur[now] = 1;
  for (p = last; p != -1 && !st[p].next[c]; p = st[p].link) st[p].next[c] = now;
  if (p == -1)
    st[now].link = 0;
  else {
    q = st[p].next[c];
    if (st[p].len + 1 == st[q].len)
      st[now].link = q;
    else {
      cl = ++siz, st[cl].len = st[p].len + 1;
      st[cl].link = st[q].link;
      memcpy(st[cl].next, st[q].next, sizeof(st[cl].next));
      for (; p != -1 && st[p].next[c] == q; p = st[p].link) st[p].next[c] = cl;
      st[now].link = st[q].link = cl;
    }
  }
  last = now;
}
vector<long long int> adj[200005];
void process(long long int x) {
  long long int i, j;
  for (i = 1; i <= siz; i++) {
    long long int p = st[i].len;
    adj[p].push_back(i);
  }
  for (i = x; i >= 1; i--) {
    for (j = 0; j < adj[i].size(); j++) {
      long long int p = adj[i][j];
      long long int p1 = st[p].link;
      occur[p1] += occur[p];
    }
  }
}
long long int dp[200005], ans;
void dfs(long long int u) {
  long long int i;
  if (u != 0) ans += (occur[u] * occur[u]);
  for (i = 0; i < 26; i++) {
    long long int p = st[u].next[i];
    if (p == 0) continue;
    dfs(p);
  }
}
int main() {
  long long int tc, i, j;
  string t;
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> tc;
  while (tc--) {
    cin >> t;
    siz = last = 0;
    st[0].link = -1;
    for (i = 0; i < t.size(); i++) inser(t[i] - 'a');
    process(t.size());
    ans = 0;
    for (i = 1; i <= siz; i++) {
      long long int p = occur[i] * occur[i];
      p = p * (st[i].len - st[st[i].link].len);
      ans += p;
    }
    cout << ans << endl;
    for (i = 0; i <= siz; i++) {
      adj[i].clear();
      occur[i] = 0;
      st[i].len = st[i].link = 0;
      for (j = 0; j < 26; j++) st[i].next[j] = 0;
    }
  }
}
