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;
const long long mod = 1e9 + 7;
const long long mod2 = 998244353;
const int inf = 0x3f3f3f3f;
const double tiaohe = 0.57721566490153286060651209;
long long oula(long long x) {
  long long res = x;
  for (register int i = 2; i <= x / i; ++i) {
    if (x % i == 0) {
      res = res / i * (i - 1);
      while (x % i == 0) x /= i;
    }
  }
  if (x > 1) res = res / x * (x - 1);
  return res;
}
long long quickmod(long long a, long long n, long long m) {
  long long s = 1;
  while (n) {
    if (n & 1) {
      s = s * a % m;
    }
    a = (a * a) % m;
    n = n / 2;
  }
  return s;
}
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
void ex_gcd(long long a, long long b, long long &x, long long &y,
            long long &d) {
  if (!b) {
    d = a, x = 1, y = 0;
  } else {
    ex_gcd(b, a % b, y, x, d);
    y -= x * (a / b);
  }
}
long long inv(long long t, long long p) {
  long long d, x, y;
  ex_gcd(t, p, x, y, d);
  return d == 1 ? (x % p + p) % p : -1;
}
bool isPrime(long long x) {
  if (x == 2) return true;
  if (x % 2 == 0) return false;
  for (long long i = 2; i * i <= x; i++)
    if (x % i == 0) return false;
  return true;
}
inline int in() {
  char ch = getchar();
  int x = 0, f = 1;
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
double eqa = (1 + sqrt(5.0)) / 2.0;
const int N = 1e5 + 5;
struct Suffix_Automata {
  int maxlen[N << 1], trans[N << 1][28], link[N << 1], A[N << 1], tmp[N << 1],
      num[N << 1], Size, Last;
  void init() {
    Size = Last = 1;
    memset(trans, 0, sizeof trans);
    memset(tmp, 0, sizeof tmp);
    memset(num, 0, sizeof num);
  }
  void Qsort() {
    for (int i = 1; i <= Size; i++) tmp[maxlen[i]]++;
    for (int i = 1; i <= Size; i++) tmp[i] += tmp[i - 1];
    for (int i = 1; i <= Size; i++) A[tmp[maxlen[i]]--] = i;
    for (int i = Size; i >= 1; i--) num[link[A[i]]] += num[A[i]];
  }
  inline void Extend(int id) {
    int cur = (++Size), p;
    num[cur] = 1;
    maxlen[cur] = maxlen[Last] + 1;
    for (p = Last; p && !trans[p][id]; p = link[p]) trans[p][id] = cur;
    if (!p)
      link[cur] = 1;
    else {
      int q = trans[p][id];
      if (maxlen[q] == maxlen[p] + 1)
        link[cur] = q;
      else {
        int clone = (++Size);
        maxlen[clone] = maxlen[p] + 1;
        memcpy(trans[clone], trans[q], sizeof trans[q]);
        link[clone] = link[q];
        for (; p && trans[p][id] == q; p = link[p]) trans[p][id] = clone;
        link[cur] = link[q] = clone;
      }
    }
    Last = cur;
  }
  void work() {
    long long ans = 0;
    for (register int i = 2; i <= Size; ++i) {
      ans += (long long)(maxlen[i] - maxlen[link[i]]) * (num[i]) * (num[i]);
    }
    cout << ans << endl;
  }
} sam;
char s[N];
int main() {
  int t = in();
  while (t--) {
    sam.init();
    scanf("%s", s + 1);
    int len = strlen(s + 1);
    for (register int i = 1; i <= len; ++i) sam.Extend(s[i] - 'a');
    sam.Qsort();
    sam.work();
  }
  return 0;
}