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 = 1e5 + 5, L = 20;
int suf[L][N], cl[L][N], ct[N], lcp[N], pos[N], lb[N], rb[N];
string s;
int n;
void build() {
  memset(ct, 0, sizeof(ct));
  for (int i = 0; i < n; i++) {
    cl[0][i] = s[i];
    ct[s[i]]++;
    suf[0][i] = i;
  }
  for (int i = 1; i < N; i++) {
    ct[i] += ct[i - 1];
  }
  for (int i = n - 1; i >= 0; i--) {
    suf[0][--ct[s[i]]] = i;
  }
  for (int i = 1; i < L; i++) {
    memset(ct, 0, sizeof(ct));
    for (int j = 0; j < n; j++) {
      suf[i - 1][j] = (suf[i - 1][j] - (1 << (i - 1)) % n + n) % n;
      ct[cl[i - 1][j]]++;
    }
    for (int j = 1; j < N; j++) {
      ct[j] += ct[j - 1];
    }
    for (int j = n - 1; j >= 0; j--) {
      suf[i][--ct[cl[i - 1][suf[i - 1][j]]]] = suf[i - 1][j];
    }
    for (int j = 0, k = 0; j < n; j++) {
      cl[i][suf[i][j]] = k;
      if (j < n - 1 && cl[i - 1][suf[i][j]] != cl[i - 1][suf[i][j + 1]] ||
          cl[i - 1][(suf[i][j] + (1 << (i - 1))) % n] !=
              cl[i - 1][(suf[i][j + 1] + (1 << (i - 1))) % n]) {
        k++;
      }
    }
  }
  for (int i = 0; i < n; i++) {
    pos[suf[L - 1][i]] = i;
  }
  for (int i = 0, k = 0; i < n; i++) {
    int j = pos[i];
    if (j == n - 1) {
      k = 0;
      continue;
    }
    j = suf[L - 1][j + 1];
    while (i + k < n && j + k < n && s[i + k] == s[j + k]) k++;
    lcp[pos[i]] = k;
    k = max(0, k - 1);
  }
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    cin >> s;
    s += "#";
    n = s.size();
    build();
    vector<int> v;
    for (int i = 0; i < n; i++) {
      while (!v.empty() && lcp[v.back()] >= lcp[i]) v.pop_back();
      lb[i] = v.empty() ? -1 : v.back();
      v.push_back(i);
    }
    v.clear();
    for (int i = n - 1; i >= 0; i--) {
      while (!v.empty() && lcp[v.back()] > lcp[i]) v.pop_back();
      rb[i] = v.empty() ? n : v.back();
      v.push_back(i);
    }
    long long ans = 0;
    for (int i = 0; i < n; i++) {
      int anime = 0;
      if (lb[i] != -1) anime = max(anime, lcp[lb[i]]);
      if (rb[i] != n) anime = max(anime, lcp[rb[i]]);
      long long len = rb[i] - lb[i];
      ans += len * len * (lcp[i] - anime);
      if (i > 0) anime = max(anime, lcp[i - 1]);
      anime = max(anime, lcp[i]);
      ans += n - suf[L - 1][i] - 1 - anime;
    }
    cout << ans << "\n";
  }
  return 0;
}