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;
const int maxN = 1e5 + 5;
const int LOG = 20;
const long long INF = 1e18;
const long long MOD = 1e9 + 7;
long long gcd(long long a, long long b) { return !b ? a : gcd(b, a % b); }
long long sq(long long x) { return (x * x) % MOD; }
long long modP(long long a, long long b) {
  return (!b ? 1 : (sq(modP(a, b / 2)) * (b % 2 ? a : 1)) % MOD);
}
int a[maxN], n;
int l[maxN], r[maxN];
int R[maxN][LOG];
int p[maxN], pw;
stack<int> st;
bool cmp(int x, int y) {
  if (R[x][pw - 1] != R[y][pw - 1]) return (R[x][pw - 1] < R[y][pw - 1]);
  if (max(x, y) + (1 << (pw - 1)) > n) return (x > y);
  return (R[x + (1 << (pw - 1))][pw - 1] < R[y + (1 << (pw - 1))][pw - 1]);
}
int LCP(int x, int y) {
  int ans = 0;
  for (int i = LOG - 1; i > -1; i--) {
    if (R[x][i] == R[y][i]) {
      ans += (1 << i);
      x += (1 << i);
      y += (1 << i);
    }
  }
  return ans;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int q;
  cin >> q;
  string s;
  while (q--) {
    cin >> s;
    n = s.size();
    for (int i = 1; i <= n; i++) {
      R[i][0] = s[i - 1];
      p[i] = i;
    }
    for (pw = 1; pw < LOG; pw++) {
      sort(p + 1, p + n + 1, cmp);
      R[p[1]][pw] = 1;
      for (int i = 2; i <= n; i++) {
        R[p[i]][pw] = R[p[i - 1]][pw] + cmp(p[i - 1], p[i]);
      }
    }
    for (int i = 1; i <= n; i++) {
      a[i - 1] = LCP(p[i], p[i + 1]);
    }
    for (int i = 0; i < n - 1; i++) {
      while (!st.empty() && a[st.top()] > a[i]) st.pop();
      if (st.empty())
        l[i] = -1;
      else
        l[i] = st.top();
      st.push(i);
    }
    while (!st.empty()) st.pop();
    for (int i = n - 2; i > -1; i--) {
      while (!st.empty() && a[st.top()] >= a[i]) st.pop();
      if (st.empty())
        r[i] = n - 1;
      else
        r[i] = st.top();
      st.push(i);
    }
    while (!st.empty()) st.pop();
    long long ans = 0;
    for (int i = 0; i < n - 1; i++) {
      ans += 1ll * (i - l[i]) * (r[i] - i) * a[i];
    }
    cout << 2 * ans + (1ll * n * n + n) / 2 << endl;
  }
  return 0;
}
