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;
char T[200100];
int n;
int RA[200100], tempRA[200100];
int SA[200100], tempSA[200100];
int c[200100];
int Phi[200100];
int PLCP[200100];
int LCP[200100];
void countingSort(int k) {
  int i, sum, maxi = max(300, n);
  memset(c, 0, sizeof c);
  for (i = 0; i < n; i++) c[i + k < n ? RA[i + k] : 0]++;
  for (i = sum = 0; i < maxi; i++) {
    int t = c[i];
    c[i] = sum;
    sum += t;
  }
  for (i = 0; i < n; i++)
    tempSA[c[SA[i] + k < n ? RA[SA[i] + k] : 0]++] = SA[i];
  for (i = 0; i < n; i++) SA[i] = tempSA[i];
}
void constructSA() {
  int i, k, r;
  for (i = 0; i < n; i++) RA[i] = T[i];
  for (i = 0; i < n; i++) SA[i] = i;
  for (k = 1; k < n; k <<= 1) {
    countingSort(k);
    countingSort(0);
    tempRA[SA[0]] = r = 0;
    for (i = 1; i < n; i++)
      tempRA[SA[i]] =
          (RA[SA[i]] == RA[SA[i - 1]] && RA[SA[i] + k] == RA[SA[i - 1] + k])
              ? r
              : ++r;
    for (i = 0; i < n; i++) RA[i] = tempRA[i];
    if (RA[SA[n - 1]] == n - 1) break;
  }
}
void computeLCP() {
  int i, L;
  Phi[SA[0]] = -1;
  for (i = 1; i < n; i++) Phi[SA[i]] = SA[i - 1];
  for (i = L = 0; i < n; i++) {
    if (Phi[i] == -1) {
      PLCP[i] = 0;
      continue;
    }
    while (T[i + L] == T[Phi[i] + L]) L++;
    PLCP[i] = L;
    L = max(L - 1, 0);
  }
  for (i = 0; i < n; i++) LCP[i] = PLCP[SA[i]];
}
int tc;
set<pair<int, int> > s;
vector<int> by[200005];
long long calc(int x) {
  if (x == 0) return 0ll;
  return (long long)(x + 1) * (long long)(x + 1);
}
int main() {
  scanf("%d", &tc);
  while (tc--) {
    memset(T, 0, sizeof(T));
    scanf("%s", T);
    n = strlen(T);
    T[n++] = '$';
    s.clear();
    constructSA();
    computeLCP();
    long long ans = 0ll;
    LCP[n] = 0;
    for (int i = 1; i < n; i++) {
      ans += max(0, n - SA[i] - 1 - max(LCP[i], LCP[i + 1]));
    }
    long long cur = calc(n - 1);
    s.insert(make_pair(n - 1, 1));
    for (int i = 0; i <= n; i++) by[i].clear();
    for (int i = 1; i < n; i++) {
      by[LCP[i]].push_back(i);
    }
    for (int l = 0; l <= n; l++) {
      for (int x : by[l]) {
        set<pair<int, int> >::iterator it =
            s.lower_bound(make_pair(x, -1000000000));
        if (it == s.end()) continue;
        int b = it->second, e = it->first;
        assert(b <= x && x <= e);
        s.erase(it);
        cur -= calc(e - b + 1);
        if (b < x) s.insert(make_pair(x - 1, b)), cur += calc(x - b);
        if (e > x) s.insert(make_pair(e, x + 1)), cur += calc(e - x);
      }
      if (s.empty()) break;
      ans += cur;
    }
    cout << ans << '\n';
  }
}