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;
int read() {
  int res = 0, f = 1;
  char c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-') f = -1;
  for (; isdigit(c); c = getchar()) res = (res << 1) + (res << 3) + (c ^ '0');
  return res * f;
}
void get(char *s) {
  char c = getchar();
  while (c < 33 || c > 126) c = getchar();
  while (33 <= c && c <= 126) *s++ = c, c = getchar();
  *s = 0;
}
const int N = 410000;
char str[N];
int Right[N], ch[N][26], len[N], prt[N];
int node, rt, Lst, n;
int newnode(int d) {
  int x = ++node;
  memset(ch[x], 0, sizeof(ch[x]));
  Right[x] = 0;
  len[x] = d;
  prt[x] = 0;
  return x;
}
void Insert(int w) {
  int p = Lst, nx = newnode(len[p] + 1);
  Lst = nx;
  for (; p && !ch[p][w]; p = prt[p]) ch[p][w] = nx;
  if (!p)
    prt[nx] = rt;
  else {
    int q = ch[p][w];
    if (len[q] == len[p] + 1)
      prt[nx] = q;
    else {
      int newq = newnode(len[p] + 1);
      for (int i = 0; i < 26; ++i) ch[newq][i] = ch[q][i];
      prt[newq] = prt[q];
      prt[q] = prt[nx] = newq;
      for (; p && ch[p][w] == q; p = prt[p]) ch[p][w] = newq;
    }
  }
}
vector<int> T[N];
void DFS(int u) {
  for (int i = 0; i < T[u].size(); ++i) {
    int to = T[u][i];
    DFS(to);
    Right[u] += Right[to];
  }
}
void solve() {
  node = 0;
  rt = Lst = newnode(0);
  get(str);
  n = strlen(str);
  for (int i = 0; i < n; ++i) Insert(str[i] - 'a'), Right[Lst] = 1;
  for (int i = 2; i <= node; ++i) T[prt[i]].push_back(i);
  DFS(rt);
  long long Ans = 0;
  for (int i = 2; i <= node; ++i)
    Ans += 1ll * (len[i] - len[prt[i]]) * Right[i] * Right[i];
  cout << Ans << '\n';
  for (int i = 1; i <= node; ++i) T[i].clear();
}
int main() {
  int T = read();
  while (T--) solve();
  return 0;
}
