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;
vector<long long> sorted_cyclic_shifts(string const& s) {
  long long n = s.size();
  vector<long long> p(n), c(n), pn(n), cn(n), cnt(n + 256, 0);
  for (long long i = 0; i < n; i++) cnt[s[i]]++;
  for (long long i = 1; i < 256; i++) cnt[i] += cnt[i - 1];
  for (long long i = 0; i < n; i++) p[--cnt[s[i]]] = i;
  c[p[0]] = 0;
  long long classes = 1;
  for (long long i = 1; i < n; i++) {
    if (s[p[i]] != s[p[i - 1]]) classes++;
    c[p[i]] = classes - 1;
  }
  for (long long h = 0; (1 << h) < n; h++) {
    for (long long i = 0; i < n; i++) {
      pn[i] = p[i] - (1 << h);
      if (pn[i] < 0) pn[i] += n;
    }
    fill(cnt.begin(), cnt.begin() + classes, 0);
    for (long long i = 0; i < n; i++) cnt[c[pn[i]]]++;
    for (long long i = 1; i < classes; i++) cnt[i] += cnt[i - 1];
    for (long long i = n - 1; i >= 0; i--) p[--cnt[c[pn[i]]]] = pn[i];
    cn[p[0]] = 0;
    classes = 1;
    for (long long i = 1; i < n; i++) {
      pair<long long, long long> cur = {c[p[i]], c[(p[i] + (1 << h)) % n]};
      pair<long long, long long> prev = {c[p[i - 1]],
                                         c[(p[i - 1] + (1 << h)) % n]};
      if (cur != prev) classes++;
      cn[p[i]] = classes - 1;
    }
    c.swap(cn);
  }
  return p;
}
vector<long long> suffix_array_construction(string s) {
  s += "#";
  vector<long long> sorted_shifts = sorted_cyclic_shifts(s);
  sorted_shifts.erase(sorted_shifts.begin());
  return sorted_shifts;
}
vector<long long> lcp_construction(string const& s,
                                   vector<long long> const& p) {
  long long n = s.size();
  vector<long long> rank(n, 0);
  for (long long i = 0; i < n; i++) rank[p[i]] = i;
  long long k = 0;
  vector<long long> lcp(n - 1, 0);
  for (long long i = 0; i < n; i++) {
    if (rank[i] == n - 1) {
      k = 0;
      continue;
    }
    long long j = p[rank[i] + 1];
    while (i + k < n and j + k < n and s[i + k] == s[j + k]) k++;
    lcp[rank[i]] = k;
    if (k) k--;
  }
  return lcp;
}
void solve() {
  string s;
  cin >> s;
  long long n = s.length();
  vector<long long> p = suffix_array_construction(s);
  vector<long long> lcp = lcp_construction(s, p);
  set<long long> pos;
  pos.insert(0);
  pos.insert(s.size());
  vector<pair<long long, long long>> v;
  for (long long i = 0; i < n - 1; i++) {
    v.push_back({lcp[i], i + 1});
  }
  sort(v.begin(), v.end());
  long long ans = 0;
  for (long long i = 0; i < n - 1; i++) {
    long long val = v[i].first;
    pos.insert(v[i].second);
    auto it = pos.find(v[i].second);
    it--;
    long long l = *it;
    it++;
    it++;
    long long r = *it;
    ans += (val) * (v[i].second - l) * (r - v[i].second);
  }
  ans *= 2;
  ans += (n * (n + 1)) / 2;
  cout << ans << "\n";
}
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  long long t;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}