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<vector<int> > c;
vector<int> sort_cyclic_shifts(string const& s) {
  int n = s.size();
  const int alphabet = 256;
  vector<int> p(n), cnt(alphabet, 0);
  c.clear();
  c.emplace_back();
  c[0].resize(n);
  for (int i = 0; i < n; i++) cnt[s[i]]++;
  for (int i = 1; i < alphabet; i++) cnt[i] += cnt[i - 1];
  for (int i = 0; i < n; i++) p[--cnt[s[i]]] = i;
  c[0][p[0]] = 0;
  int classes = 1;
  for (int i = 1; i < n; i++) {
    if (s[p[i]] != s[p[i - 1]]) classes++;
    c[0][p[i]] = classes - 1;
  }
  vector<int> pn(n), cn(n);
  cnt.resize(n);
  for (int h = 0; (1 << h) < n; ++h) {
    for (int i = 0; i < n; i++) {
      pn[i] = p[i] - (1 << h);
      if (pn[i] < 0) pn[i] += n;
    }
    fill(cnt.begin(), cnt.end(), 0);
    for (int i = 0; i < n; i++) cnt[c[h][pn[i]]]++;
    for (int i = 1; i < classes; i++) cnt[i] += cnt[i - 1];
    for (int i = n - 1; i >= 0; i--) p[--cnt[c[h][pn[i]]]] = pn[i];
    cn[p[0]] = 0;
    classes = 1;
    for (int i = 1; i < n; i++) {
      pair<int, int> cur = {c[h][p[i]], c[h][(p[i] + (1 << h)) % n]};
      pair<int, int> prev = {c[h][p[i - 1]], c[h][(p[i - 1] + (1 << h)) % n]};
      if (cur != prev) ++classes;
      cn[p[i]] = classes - 1;
    }
    c.push_back(cn);
  }
  return p;
}
vector<int> suffix_array_construction(string s) {
  s += "!";
  vector<int> sorted_shifts = sort_cyclic_shifts(s);
  sorted_shifts.erase(sorted_shifts.begin());
  return sorted_shifts;
}
int compare(int i, int j, int n, int k) {
  pair<int, int> a = {c[k][i], c[k][(i + 1 - (1 << k)) % n]};
  pair<int, int> b = {c[k][j], c[k][(j + 1 - (1 << k)) % n]};
  return a == b ? 0 : a < b ? -1 : 1;
}
int lcp(int i, int j) {
  int log_n = c.size() - 1;
  int ans = 0;
  for (int k = log_n; k >= 0; k--) {
    if (c[k][i] == c[k][j]) {
      ans += 1 << k;
      i += 1 << k;
      j += 1 << k;
    }
  }
  return ans;
}
vector<int> lcp_construction(string const& s, vector<int> const& p) {
  int n = s.size();
  vector<int> rank(n, 0);
  for (int i = 0; i < n; i++) rank[p[i]] = i;
  int k = 0;
  vector<int> lcp(n - 1, 0);
  for (int i = 0; i < n; i++) {
    if (rank[i] == n - 1) {
      k = 0;
      continue;
    }
    int j = p[rank[i] + 1];
    while (i + k < n && j + k < n && s[i + k] == s[j + k]) k++;
    lcp[rank[i]] = k;
    if (k) k--;
  }
  return lcp;
}
const int MAX = 100000;
struct Node {
  long long sqsum, linsum;
  long long lazyadd;
  bool lazypaint;
};
Node tr[4 * MAX];
void init(int u, int l, int r) {
  tr[u].lazyadd = tr[u].linsum = tr[u].sqsum = tr[u].lazypaint = 0;
  if (l == r) return;
  int mid = (l + r) / 2;
  init(u * 2, l, mid);
  init(u * 2 + 1, mid + 1, r);
}
void propagate(int u, int l, int r) {
  assert(!tr[u].lazyadd || !tr[u].lazypaint);
  if (tr[u].lazyadd) {
    tr[u].sqsum += 2 * tr[u].linsum * tr[u].lazyadd +
                   (r - l + 1) * tr[u].lazyadd * tr[u].lazyadd;
    tr[u].linsum += tr[u].lazyadd * (r - l + 1);
    if (l != r) {
      int mid = (l + r) / 2;
      if (tr[u * 2].lazypaint) propagate(u * 2, l, mid);
      if (tr[u * 2 + 1].lazypaint) propagate(u * 2 + 1, mid + 1, r);
      tr[u * 2].lazyadd += tr[u].lazyadd;
      tr[u * 2 + 1].lazyadd += tr[u].lazyadd;
    }
    tr[u].lazyadd = 0;
  } else if (tr[u].lazypaint) {
    tr[u].sqsum = 0;
    tr[u].linsum = 0;
    if (l != r) {
      int mid = (l + r) / 2;
      tr[u * 2].lazyadd = 0;
      tr[u * 2 + 1].lazyadd = 0;
      tr[u * 2].lazypaint = true;
      tr[u * 2 + 1].lazypaint = true;
    }
    tr[u].lazypaint = false;
  }
}
void paintRange(int u, int l, int r, int x, int y) {
  propagate(u, l, r);
  if (y < l || r < x) return;
  if (x <= l && r <= y) {
    tr[u].lazypaint = true;
    propagate(u, l, r);
    return;
  }
  int mid = (l + r) / 2;
  paintRange(u * 2, l, mid, x, y);
  paintRange(u * 2 + 1, mid + 1, r, x, y);
  tr[u].sqsum = tr[u * 2].sqsum + tr[u * 2 + 1].sqsum;
  tr[u].linsum = tr[u * 2].linsum + tr[u * 2 + 1].linsum;
}
void addRange(int u, int l, int r, int x, int y) {
  propagate(u, l, r);
  if (y < l || r < x) return;
  if (x <= l && r <= y) {
    tr[u].lazyadd++;
    propagate(u, l, r);
    return;
  }
  int mid = (l + r) / 2;
  addRange(u * 2, l, mid, x, y);
  addRange(u * 2 + 1, mid + 1, r, x, y);
  tr[u].sqsum = tr[u * 2].sqsum + tr[u * 2 + 1].sqsum;
  tr[u].linsum = tr[u * 2].linsum + tr[u * 2 + 1].linsum;
}
long long queryRange(int u, int l, int r, int x, int y) {
  propagate(u, l, r);
  if (y < l || r < x) return 0;
  if (x <= l && r <= y) return tr[u].sqsum;
  int mid = (l + r) / 2;
  return queryRange(u * 2, l, mid, x, y) +
         queryRange(u * 2 + 1, mid + 1, r, x, y);
}
int main() {
  std::ios_base::sync_with_stdio(false);
  int T;
  cin >> T;
  while (T--) {
    string s;
    cin >> s;
    vector<int> p = suffix_array_construction(s);
    vector<int> lcp = lcp_construction(s, p);
    int N = s.size();
    init(1, 1, N);
    long long ans = 0;
    for (int i = 0; i < p.size(); i++) {
      int R = N - p[i];
      int L = 1 + (i > 0 ? lcp[i - 1] : 0);
      ans += queryRange(1, 1, N, L, N);
      paintRange(1, 1, N, L, N);
      addRange(1, 1, N, 1, R);
    }
    ans += queryRange(1, 1, N, 1, N);
    cout << ans << endl;
  }
  return 0;
}