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 IN() {
  int c, f, x;
  while (!isdigit(c = getchar()) && c != '-')
    ;
  c == '-' ? (f = 1, x = 0) : (f = 0, x = c - '0');
  while (isdigit(c = getchar())) x = (x << 1) + (x << 3) + c - '0';
  return !f ? x : -x;
}
const int N = 200000 + 19;
struct node {
  node *par, *go[26];
  int val, R;
} Nd[N], *cur, *rt, *las, *Q[N];
char s[N];
int cnt[N];
int n;
long long res;
node *New(int val) {
  memset(cur->go, 0, sizeof(cur->go));
  cur->par = 0;
  cur->val = val;
  cur->R = 0;
  return cur++;
}
void Extend(int x) {
  node *p = las, *np = New(p->val + 1);
  for (; p && !p->go[x]; p = p->par) p->go[x] = np;
  if (!p)
    np->par = rt;
  else {
    node *t = p->go[x];
    if (t->val == p->val + 1)
      np->par = t;
    else {
      node *nt = New(p->val + 1);
      nt->par = t->par;
      t->par = np->par = nt;
      memcpy(nt->go, t->go, sizeof(t->go));
      for (; p && p->go[x] == t; p = p->par) p->go[x] = nt;
    }
  }
  las = np;
}
void Main() {
  cur = Nd;
  rt = las = New(0);
  scanf("%s", s + 1);
  n = strlen(s + 1);
  for (int i = 1; i < n + 1; i++) {
    Extend(s[i] - 'a');
    las->R++;
  }
  memset(cnt, 0, sizeof(cnt));
  for (node *i = Nd; i < cur; i++) cnt[i->val]++;
  for (int i = 1; i < n + 1; i++) cnt[i] += cnt[i - 1];
  for (node *i = Nd; i < cur; i++) Q[cnt[i->val]--] = i;
  for (int i = cur - Nd; i > 1; i--) Q[i]->par->R += Q[i]->R;
  res = 0;
  for (node *i = Nd + 1; i < cur; i++) {
    res += 1ll * (i->val - i->par->val) * (i->R) * (i->R);
  }
  cout << res << endl;
}
int main() {
  for (int T = IN(); T--;) Main();
}
