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;
char str[100005];
int rnk[100005], sa[100005], lcp[100005];
pair<pair<int, int>, int> tmp[100005];
pair<int, int> st[100005];
void solve() {
  scanf("%s", &str);
  int n = strlen(str);
  for (int i = 0; i < n; i++) rnk[i] = str[i] - 'a';
  for (int k = 1; k <= n; k <<= 1) {
    for (int i = 0; i < n; i++) {
      tmp[i] = pair<pair<int, int>, int>(
          pair<int, int>(rnk[i], i + k < n ? rnk[i + k] : -1), i);
    }
    sort(tmp, tmp + n);
    for (int i = 0; i < n;) {
      int f = i;
      for (; i < n && tmp[i].first == tmp[f].first; i++) rnk[tmp[i].second] = f;
    }
  }
  for (int i = 0; i < n; i++) sa[rnk[i]] = i;
  int H = 0;
  for (int i = 0; i < n; i++) {
    if (H > 0) H--;
    if (rnk[i] == n - 1) continue;
    int to = sa[rnk[i] + 1];
    while (i + H < n && to + H < n && str[i + H] == str[to + H]) H++;
    lcp[rnk[i]] = H;
  }
  int sz = 0;
  st[sz++] = pair<int, int>(n - sa[0], 0);
  long long int ret = 0;
  for (int i = 1; i < n; i++) {
    int back = lcp[i - 1];
    int last = i - 1;
    while (sz > 0 && st[sz - 1].first >= back) {
      int nxt = sz == 1 ? 0 : st[sz - 2].first;
      nxt = max(nxt, back);
      ret += (long long int)(st[sz - 1].first - nxt) *
             (long long int)(i - st[sz - 1].second) *
             (long long int)(i - st[sz - 1].second);
      last = st[sz - 1].second;
      sz--;
    }
    st[sz++] = pair<int, int>(back, last);
    if (back < n - sa[i]) st[sz++] = pair<int, int>(n - sa[i], i);
  }
  while (sz > 0) {
    int nxt = sz == 1 ? 0 : st[sz - 2].first;
    ret += (long long int)(st[sz - 1].first - nxt) *
           (long long int)(n - st[sz - 1].second) *
           (long long int)(n - st[sz - 1].second);
    sz--;
  }
  printf("%lld\n", ret);
}
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    solve();
  }
  return 0;
}
