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;
long long int inf = 1000000ll * 1000 + 200;
long long int mod = 1e9 + 7;
const int maxn = 2e5 + 20;
vector<long long int> vec[maxn];
long long int hsh[maxn], p[maxn];
long long int cnt[maxn], l[maxn], r[maxn], res, sufarr[maxn], alive[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]);
  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;
}
bool cmp(int i, int j) {
  int tmp = lcp(i, j);
  return (int(s[i + tmp - 1]) < int(s[j + tmp - 1]));
}
void uni(int x, int y) {
  res -= cnt[x] * cnt[x] + cnt[y] * cnt[y];
  res += (cnt[x] + cnt[y]) * (cnt[x] + cnt[y]);
  l[r[y]] = l[x];
  r[l[x]] = r[y];
  long long int tmp = cnt[x];
  cnt[l[x]] += cnt[y];
  cnt[r[y]] += tmp;
  return;
}
void solve() {
  long long int ans = 0;
  res = 0;
  cin >> s;
  n = s.size();
  cnt[0] = l[0] = r[0] = 0;
  for (int i = 1; i <= n; i++) {
    cnt[i] = 0;
    l[i] = r[i] = i;
    hsh[i] = (hsh[i - 1] * 727 + int(s[i - 1]));
  }
  iota(sufarr, sufarr + n, 1);
  sort(sufarr, sufarr + n, cmp);
  for (int i = 0; i + 1 < n; i++) {
    alive[n - sufarr[i] + 1] = i;
    int tmp = lcp(sufarr[i], sufarr[i + 1]);
    if (tmp) {
      vec[tmp].push_back(i);
    }
  }
  alive[n - sufarr[n - 1] + 1] = n - 1;
  for (int i = n; i > 0; i--) {
    cnt[alive[i]] = 1;
    res += 1;
    for (int j : vec[i]) {
      uni(j, j + 1);
    }
    vec[i].clear();
    ans += res;
  }
  cout << ans << "\n";
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int q;
  cin >> q;
  p[0] = 1;
  for (int i = 1; i < maxn; i++) {
    p[i] = (1ll * p[i - 1] * 727);
  }
  while (q--) {
    solve();
  }
  return 0;
}