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;
struct suffix {
  long long ind;
  long long rank[2];
};
bool comp(suffix a, suffix b) {
  if (a.rank[0] == b.rank[0]) return a.rank[1] < b.rank[1];
  return a.rank[0] < b.rank[0];
}
void calsuffixarray(string s, vector<long long> &suffixarr) {
  long long n;
  n = s.size();
  vector<suffix> suffixes(n);
  for (int i = 0; i < n; i++) {
    suffixes[i].ind = i;
    suffixes[i].rank[0] = s[i] - 'a';
    suffixes[i].rank[1] = ((i + 1) < n) ? s[i + 1] - 'a' : -1;
  }
  sort(suffixes.begin(), suffixes.end(), comp);
  vector<long long> ind(n);
  for (long long k = 4; k < 2 * n; k = k * 2) {
    long long rank = 0;
    long long prev_rank = suffixes[0].rank[0];
    suffixes[0].rank[0] = rank;
    ind[suffixes[0].ind] = 0;
    for (int i = 1; i < n; i++) {
      if (suffixes[i].rank[0] == prev_rank &&
          suffixes[i].rank[1] == suffixes[i - 1].rank[1]) {
        prev_rank = suffixes[i].rank[0];
        suffixes[i].rank[0] = rank;
      } else {
        prev_rank = suffixes[i].rank[0];
        suffixes[i].rank[0] = ++rank;
      }
      ind[suffixes[i].ind] = i;
    }
    for (int i = 0; i < n; i++) {
      long long nextindex = suffixes[i].ind + k / 2;
      suffixes[i].rank[1] =
          (nextindex < n) ? suffixes[ind[nextindex]].rank[0] : -1;
    }
    sort(suffixes.begin(), suffixes.end(), comp);
  }
  for (int i = 0; i < n; i++) {
    suffixarr[i] = suffixes[i].ind;
  }
}
void callcparray(string s, vector<long long> &suffixarr,
                 vector<long long> &lcp) {
  long long n = suffixarr.size();
  vector<long long> invsuff(n, 0);
  for (int i = 0; i < n; i++) {
    invsuff[suffixarr[i]] = i;
  }
  long long k = 0;
  for (int i = 0; i < n; i++) {
    if (invsuff[i] == n - 1) {
      k = 0;
      continue;
    }
    long long j = suffixarr[invsuff[i] + 1];
    while (i + k < n && j + k < n && s[i + k] == s[j + k]) {
      k++;
    }
    lcp[invsuff[i]] = k;
    if (k > 0) k--;
  }
}
const int N = 1e5 + 5;
const int LN = 18;
int t;
char str[N];
int n;
int sa[LN][N];
int arr[N];
int lcp[N];
pair<pair<int, int>, int> tmp[N];
void buildsa() {
  for (int i = 1; i <= n; ++i) {
    sa[0][i] = str[i] - 'a' + 1;
  }
  for (int i = 1; i < LN; ++i) {
    for (int j = 1; j <= n; ++j) {
      int x = j + (1 << (i - 1));
      tmp[j] = {{sa[i - 1][j], (x > n) ? -1 : sa[i - 1][x]}, j};
    }
    sort(tmp + 1, tmp + 1 + n);
    sa[i][tmp[1].second] = 1;
    for (int j = 2; j <= n; ++j) {
      sa[i][tmp[j].second] =
          sa[i][tmp[j - 1].second] + (tmp[j].first != tmp[j - 1].first);
    }
  }
  for (int i = 1; i <= n; ++i) {
    arr[sa[LN - 1][i]] = i;
  }
  for (int i = 1; i < n; ++i) {
    int x = arr[i];
    int y = arr[i + 1];
    int res = 0;
    for (int j = LN - 1; j >= 0; --j) {
      if (sa[j][x] == sa[j][y]) {
        res += 1 << j;
        x += 1 << j;
        y += 1 << j;
      }
    }
    lcp[i] = res;
  }
  lcp[n] = 0;
}
int lft[N];
int rgt[N];
void solve() {
  string st;
  cin >> st;
  strcpy(str + 1, st.c_str());
  n = strlen(str + 1);
  vector<long long> suf(n + 1);
  st = "$" + st;
  calsuffixarray(st, suf);
  vector<long long> lcp(n + 1);
  callcparray(st, suf, lcp);
  long long ans = 1LL * n * (n + 1LL);
  ans >>= 1;
  stack<int> s;
  while (!s.empty()) {
    s.pop();
  }
  for (int i = 1; i < n; ++i) {
    while (!s.empty() && lcp[s.top()] >= lcp[i]) {
      s.pop();
    }
    if (s.empty()) {
      lft[i] = 1;
    } else {
      lft[i] = s.top() + 1;
    }
    s.push(i);
  }
  while (!s.empty()) {
    s.pop();
  }
  for (int i = n - 1; i >= 1; --i) {
    while (!s.empty() && lcp[s.top()] > lcp[i]) {
      s.pop();
    }
    if (s.empty()) {
      rgt[i] = n;
    } else {
      rgt[i] = s.top();
    }
    s.push(i);
  }
  for (int i = 1; i < n; ++i) {
    ans += 2LL * lcp[i] * (i - lft[i] + 1) * (rgt[i] - i);
  }
  printf("%lld\n", ans);
}
int main() {
  scanf("%d", &t);
  while (t--) {
    solve();
  }
}