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;
int mod = 1e9 + 7;
int inf = 1e9;
long long int linf = 1000000ll * 10000 * 10000;
const int maxn = 1e5 + 20;
long long int sufarr[maxn], rnk[2 * maxn], hsh[maxn], p[maxn];
int lp[maxn], l[maxn], r[maxn];
string s;
int n;
long long int gthsh(int l, int r) {
  long long int res = (hsh[r] - hsh[l - 1] * p[r - l + 1]) % mod;
  if (res < 0) res += mod;
  return res;
}
int lcp(int i, int j) {
  int l = 0, r = n - max(i, j) + 2;
  while (r - l > 1) {
    int mid = (l + r) >> 1;
    if (gthsh(i, i + mid - 1) == gthsh(j, j + mid - 1)) {
      l = mid;
    } else {
      r = mid;
    }
  }
  return l;
}
void solve() {
  fill(rnk, rnk + 2 * maxn, 0);
  fill(sufarr, sufarr + maxn, 0);
  cin >> s;
  n = s.size();
  for (int i = 1; i <= n; i++) {
    hsh[i] = (hsh[i - 1] * 727 + int(s[i - 1])) % mod;
  }
  for (int i = 0; i < n; i++) {
    rnk[i] = int(s[i]);
  }
  for (int k = 0; (1 << k) <= n; k++) {
    vector<pair<long long int, int> > vec;
    for (int i = 0; i < n; i++) {
      vec.push_back({(rnk[i] << 30) + rnk[i + (1 << k)], i});
    }
    sort(vec.begin(), vec.end());
    rnk[vec[0].second] = 1;
    for (int i = 1; i < vec.size(); i++) {
      rnk[vec[i].second] =
          rnk[vec[i - 1].second] + (vec[i].first != vec[i - 1].first);
    }
    vec.clear();
  }
  for (int i = 0; i < n; i++) {
    sufarr[rnk[i] - 1] = i;
  }
  for (int i = 0; i < n - 1; i++) {
    lp[i] = lcp(sufarr[i] + 1, sufarr[i + 1] + 1);
  }
  stack<int> stk;
  for (int i = 0; i < n - 1; i++) {
    while (!stk.empty() && lp[stk.top()] > lp[i]) {
      stk.pop();
    }
    if (stk.empty()) {
      l[i] = -1;
    } else {
      l[i] = stk.top();
    }
    stk.push(i);
  }
  while (!stk.empty()) {
    stk.pop();
  }
  for (int i = n - 2; i >= 0; i--) {
    while (!stk.empty() && lp[stk.top()] >= lp[i]) {
      stk.pop();
    }
    if (stk.empty()) {
      r[i] = n;
    } else {
      r[i] = stk.top();
    }
    stk.push(i);
  }
  for (int i = 0; i < n - 1; i++) {
  }
  while (!stk.empty()) {
    stk.pop();
  }
  long long int res = 0;
  for (int i = 0; i < n - 1; i++) {
    if (l[i] != -1 && lp[l[i]] == lp[i]) continue;
    int mx = 0;
    if (l[i] != -1) {
      mx = max(mx, lp[l[i]]);
    }
    if (r[i] != n) {
      mx = max(mx, lp[r[i]]);
    }
    long long int tmp = min(r[i], n - 1) - l[i];
    tmp *= tmp * (lp[i] - mx);
    res += tmp;
  }
  for (int i = 1; i < n - 1; i++) {
    res += n - sufarr[i] - max(lp[i - 1], lp[i]);
  }
  res += n - sufarr[0] - lp[0];
  if (n != 1) {
    res += n - sufarr[n - 1] - lp[n - 2];
  }
  cout << res << "\n";
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  p[0] = 1;
  for (int i = 1; i < maxn; i++) {
    p[i] = (p[i - 1] * 727) % mod;
  }
  int t;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
