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;
string s;
int g[100001];
int cnt[100001];
int tmp[100001];
int lcp[100001];
int s_ix[100000];
int d;
bool cmp(int a, int b) {
  if (g[a] != g[b]) return g[a] < g[b];
  return g[a + d] < g[b + d];
}
void cnt_sort(int len) {
  int i;
  memset(cnt, 0, sizeof(cnt));
  memset(tmp, 0, sizeof(tmp));
  for (i = 0; i < len; i++) cnt[g[min(len, s_ix[i] + d)]]++;
  for (i = 1; i <= 'z' || i < len; i++) cnt[i] += cnt[i - 1];
  for (i = len - 1; i >= 0; i--) tmp[--cnt[g[min(len, s_ix[i] + d)]]] = s_ix[i];
  memset(cnt, 0, sizeof(cnt));
  for (i = 0; i < len; i++) cnt[g[tmp[i]]]++;
  for (i = 1; i <= 'z' || i < len; i++) cnt[i] += cnt[i - 1];
  for (i = len - 1; i >= 0; i--) s_ix[--cnt[g[tmp[i]]]] = tmp[i];
}
int main() {
  int i, j, k;
  int T;
  int val;
  int offset;
  long long ans = 0;
  cin >> T;
  while (T--) {
    ans = 0;
    cin >> s;
    for (i = 0; i < s.size(); i++) {
      g[i] = s[i];
      s_ix[i] = i;
      ans += i + 1;
    }
    g[i] = 0;
    d = 1;
    do {
      cnt_sort(s.size());
      tmp[s_ix[0]] = 1;
      for (i = 1; i < s.size(); i++)
        tmp[s_ix[i]] = tmp[s_ix[i - 1]] + cmp(s_ix[i - 1], s_ix[i]);
      tmp[i] = 0;
      d <<= 1;
      for (i = 0; i < s.size(); i++) g[i] = tmp[i];
    } while (g[s_ix[s.size() - 1]] != s.size());
    for (i = 0; i < s.size(); i++) g[i]--;
    d = 0;
    vector<pair<int, int> > v;
    set<int> st;
    for (i = 0; i < s.size(); i++) {
      if (g[i] == 0) continue;
      j = s_ix[g[i] - 1];
      d = max(d - 1, 0);
      lcp[g[i]] = 0;
      while (i + d < s.size() && j + d < s.size() && s[i + d] == s[j + d])
        lcp[g[i]] = ++d;
      d = lcp[g[i]] - 1;
      if (lcp[g[i]] == 0)
        st.insert(g[i]);
      else
        v.push_back(make_pair(lcp[g[i]], g[i]));
    }
    lcp[i] = 0;
    st.insert(0);
    st.insert(s.size());
    sort(v.begin(), v.end());
    for (i = 0; i < v.size(); i++) {
      st.insert(v[i].second);
      auto r = st.find(v[i].second);
      auto l = st.find(v[i].second);
      r++;
      l--;
      offset = max(lcp[*l], lcp[*r]);
      val = (*r - *l);
      ans += (v[i].first - offset) * ((long long)val * val - val);
    }
    cout << ans << "\n";
  }
  return 0;
}
