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>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
using namespace std;
const int N = 1e5 + 100, LOG = 18;
string c;
int r[N][LOG], p[N], pw;
long long int n;
bool CMP(long long int x, long long 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];
}
void build() {
  for (int i = 0; i < n; i++) {
    r[i][0] = (long long int)c[i];
    p[i] = i;
  }
  for (pw = 1; pw < LOG; pw++) {
    sort(p, p + n, CMP);
    r[p[0]][pw] = 1;
    for (int i = 1; i < n; i++) {
      r[p[i]][pw] = r[p[i - 1]][pw] + CMP(p[i - 1], p[i]);
    }
  }
}
int lcp(int x, int y) {
  int res = 0;
  for (int i = LOG - 1; i >= 0; i--) {
    if (r[x][i] == r[y][i]) {
      res ^= (1 << i), x += (1 << i), y += (1 << i);
    }
  }
  return res;
}
int a[N];
int b[N];
int d[N];
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  for (int i = 0; i < t; i++) {
    cin >> c;
    n = c.size();
    build();
    for (int i = 0; i < n - 1; i++) {
      a[i + 1] = lcp(p[i], p[i + 1]);
    }
    a[n] = -1;
    vector<int> v;
    v.push_back(0);
    a[0] = -1;
    for (int i = 1; i <= n - 1; i++) {
      while (a[v.back()] > a[i]) {
        v.pop_back();
      }
      b[i] = v.back();
      v.push_back(i);
    }
    v.clear();
    v.push_back(n);
    for (int i = n - 1; i > 0; i--) {
      while (a[v.back()] >= a[i]) {
        v.pop_back();
      }
      d[i] = v.back();
      v.push_back(i);
    }
    long long int ans = 0;
    for (int i = 1; i <= n - 1; i++) {
      long long int z = i - b[i];
      long long int x = d[i] - i;
      ans += z * x * a[i];
    }
    cout << ans * 2 + (n + 1) * n / 2 << '\n';
    memset(r, 0, sizeof r);
    memset(p, 0, sizeof p);
    memset(d, 0, sizeof d);
    memset(b, 0, sizeof b);
  }
}
