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 ll = long long;
using ld = long double;
using ull = unsigned long long;
using namespace std;
struct SuffixArray {
  vector<int> sa, lcp;
  SuffixArray(string& s, int lim = 256) {
    int n = ((int)(s).size()) + 1, k = 0, a, b;
    vector<int> x((s).begin(), (s).end() + 1), y(n), ws(max(n, lim)), rank(n);
    sa = lcp = y, iota((sa).begin(), (sa).end(), 0);
    for (int j = 0, p = 0; p < n; j = max(1, j * 2), lim = p) {
      p = j, iota((y).begin(), (y).end(), n - j);
      for (int i = (0); i <= (n - 1); ++i)
        if (sa[i] >= j) y[p++] = sa[i] - j;
      fill((ws).begin(), (ws).end(), 0);
      for (int i = (0); i <= (n - 1); ++i) ws[x[i]]++;
      for (int i = (1); i <= (lim - 1); ++i) ws[i] += ws[i - 1];
      for (int i = n; i--;) sa[--ws[x[y[i]]]] = y[i];
      swap(x, y), p = 1, x[sa[0]] = 0;
      for (int i = (1); i <= (n - 1); ++i)
        a = sa[i - 1], b = sa[i],
        x[b] = (y[a] == y[b] && y[a + j] == y[b + j]) ? p - 1 : p++;
    }
    for (int i = (1); i <= (n - 1); ++i) rank[sa[i]] = i;
    for (int i = 0, j; i < n - 1; lcp[rank[i++]] = k)
      for (k &&k--, j = sa[rank[i] - 1]; s[i + k] == s[j + k]; k++)
        ;
  }
};
const int N = 1e5 + 2;
int T, top, D[N], cnt[N];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> T;
  while (T--) {
    string s;
    cin >> s;
    SuffixArray SA(s, 256);
    int n = ((int)(s).size());
    ll sum = 0, ans = n * 1LL * (n + 1) / 2;
    top = 0;
    for (int i = (2); i <= (n); ++i) {
      cnt[i] = 1;
      while (top && SA.lcp[D[top]] > SA.lcp[i])
        sum -= (SA.lcp[D[top]] - SA.lcp[i]) * cnt[D[top]],
            cnt[i] += cnt[D[top]], top--;
      sum += SA.lcp[i];
      ans += 2 * sum;
      D[++top] = i;
    }
    cout << ans << '\n';
  }
  return 0;
}
