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 = 200000 + 9;
const int SGZ = 26;
inline int read() {
  char c = getchar();
  int f = 1, ret = 0;
  while (c < '0' || c > '9') {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (c <= '9' && c >= '0') {
    ret = ret * 10 + c - '0';
    c = getchar();
  }
  return ret * f;
}
namespace Suffix_Automaton {
char pat[N];
int n, ch[N][SGZ], fail[N], sz[N], len[N], cnt, last;
vector<int> G[N];
long long ans = 0;
inline void extend(int c) {
  int w = last;
  last = ++cnt;
  len[last] = len[w] + 1;
  sz[last] = 1;
  while (w && !ch[w][c]) ch[w][c] = last, w = fail[w];
  if (!w)
    fail[last] = 1;
  else {
    int nw = ch[w][c];
    if (len[nw] == len[w] + 1)
      fail[last] = nw;
    else {
      int nnw = ++cnt;
      len[nnw] = len[w] + 1;
      for (int i = 0; i < SGZ; i++) ch[nnw][i] = ch[nw][i];
      fail[nnw] = fail[nw];
      fail[nw] = fail[last] = nnw;
      while (w && ch[w][c] == nw) ch[w][c] = nnw, w = fail[w];
    }
  }
}
void DFS(int w) {
  for (int i = 0; i < (int)G[w].size(); ++i) {
    DFS(G[w][i]);
    sz[w] += sz[G[w][i]];
  }
  ans += (long long)(len[w] - len[fail[w]]) * sz[w] * sz[w];
}
inline long long solve() {
  memset(ch, 0, sizeof(ch));
  memset(fail, 0, sizeof(fail));
  memset(sz, 0, sizeof(sz));
  memset(len, 0, sizeof(len));
  cnt = last = 1;
  scanf("%s", pat + 1);
  n = strlen(pat + 1);
  for (int i = 1; i <= n; i++) {
    extend(pat[i] - 'a');
  }
  for (int i = 1; i <= cnt; i++) {
    G[i].clear();
  }
  for (int i = 2; i <= cnt; i++) {
    G[fail[i]].push_back(i);
  }
  ans = 0;
  DFS(1);
  return ans;
}
};  // namespace Suffix_Automaton
int main() {
  for (int T = read(); T; T--) {
    cout << Suffix_Automaton::solve() << endl;
  }
  return 0;
}