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;
struct SA {
  struct state {
    int len, link, nxt[26], flag = 0;
    state() : len(0), link(-1) { fill(begin(nxt), end(nxt), 0); }
  };
  vector<state> d;
  int last = 0;
  SA() { d.push_back(state()); }
  void add(char ch) {
    int cur = d.size();
    d.push_back(state());
    d[cur].len = d[last].len + 1;
    int p = last;
    while (p != -1 && !d[p].nxt[ch - 'a']) {
      d[p].nxt[ch - 'a'] = cur;
      p = d[p].link;
    }
    if (p == -1) {
      d[cur].link = 0;
    } else {
      int q = d[p].nxt[ch - 'a'];
      if (d[q].len == d[p].len + 1) {
        d[cur].link = q;
      } else {
        int clone = d.size();
        d.push_back(d[q]);
        d[clone].flag = 1;
        d[clone].len = d[p].len + 1;
        while (p != -1 && d[p].nxt[ch - 'a'] == q) {
          d[p].nxt[ch - 'a'] = clone;
          p = d[p].link;
        }
        d[cur].link = d[q].link = clone;
      }
    }
    last = cur;
  }
} sa;
const int N = 2e5 + 7;
vector<int> adj[N];
int cnt[N];
long long dp[N];
void dfs(int v) {
  for (int u : adj[v]) {
    dfs(u);
    cnt[v] += cnt[u];
  }
}
long long f(int v) {
  if (~dp[v]) return dp[v];
  long long res = 1LL * cnt[v] * cnt[v];
  for (int i = 0; i < 26; i++)
    if (sa.d[v].nxt[i]) {
      res += f(sa.d[v].nxt[i]);
    }
  return dp[v] = res;
}
int32_t main() {
  ios_base ::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    string str;
    cin >> str;
    sa = SA();
    for (auto ch : str) sa.add(ch);
    for (int i = 0; i < sa.d.size(); i++) {
      cnt[i] = 0;
      dp[i] = -1;
      adj[i].clear();
    }
    for (int i = 1; i < sa.d.size(); i++) {
      adj[sa.d[i].link].push_back(i);
      cnt[i] = !sa.d[i].flag;
    }
    dfs(0);
    cnt[0] = 0;
    cout << f(0) << endl;
  }
}