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;
typedef long long int ll;
typedef unsigned long long int ull;
inline void smax(int &x, int y) { x = max(x, y); }
inline void smin(int &x, int y) { x = min(x, y); }
const int MAXN = 1000005;
int str[MAXN];
int _rank[MAXN], pos[MAXN];
int cnt[MAXN], _next[MAXN];
bool bh[MAXN], b2h[MAXN];
bool smaller_first_char(int a, int b) { return str[a] < str[b]; }
void suffixSort(int n) {
  for (int i = 0; i < n; ++i) {
    pos[i] = i;
  }
  sort(pos, pos + n, smaller_first_char);
  for (int i = 0; i < n; ++i) {
    bh[i] = i == 0 || str[pos[i]] != str[pos[i - 1]];
    b2h[i] = false;
  }
  for (int h = 1; h < n; h <<= 1) {
    int buckets = 0;
    for (int i = 0, j; i < n; i = j) {
      j = i + 1;
      while (j < n && !bh[j]) j++;
      _next[i] = j;
      buckets++;
    }
    if (buckets == n) break;
    for (int i = 0; i < n; i = _next[i]) {
      cnt[i] = 0;
      for (int j = i; j < _next[i]; ++j) {
        _rank[pos[j]] = i;
      }
    }
    cnt[_rank[n - h]]++;
    b2h[_rank[n - h]] = true;
    for (int i = 0; i < n; i = _next[i]) {
      for (int j = i; j < _next[i]; ++j) {
        int s = pos[j] - h;
        if (s >= 0) {
          int head = _rank[s];
          _rank[s] = head + cnt[head]++;
          b2h[_rank[s]] = true;
        }
      }
      for (int j = i; j < _next[i]; ++j) {
        int s = pos[j] - h;
        if (s >= 0 && b2h[_rank[s]]) {
          for (int k = _rank[s] + 1; !bh[k] && b2h[k]; k++) b2h[k] = false;
        }
      }
    }
    for (int i = 0; i < n; ++i) {
      pos[_rank[i]] = i;
      bh[i] |= b2h[i];
    }
  }
  for (int i = 0; i < n; ++i) {
    _rank[pos[i]] = i;
  }
}
int lcp[MAXN];
void getHeight(int n) {
  for (int i = 0; i < n; ++i) _rank[pos[i]] = i;
  lcp[0] = 0;
  for (int i = 0, h = 0; i < n; ++i) {
    if (_rank[i] > 0) {
      int j = pos[_rank[i] - 1];
      while (i + h < n && j + h < n && str[i + h] == str[j + h]) h++;
      lcp[_rank[i]] = h;
      if (h > 0) h--;
    }
  }
}
int lft[MAXN];
int rgt[MAXN];
int main() {
  int tcase;
  cin >> tcase;
  for (__typeof(tcase + 1) tc = (1) - ((1) > (tcase + 1));
       tc != (tcase + 1) - ((1) > (tcase + 1));
       tc += 1 - 2 * ((1) > (tcase + 1))) {
    string st;
    cin >> st;
    int n = st.size();
    for (int i = 0; i < n; i++) str[i] = st[i] - '0';
    suffixSort(n);
    getHeight(n);
    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);
    }
    cout << ans << "\n";
  }
}