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>
const int kN = 1e5 + 10;
int lth;
char s[kN];
inline int read() {
  int f = 1, w = 0;
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-') f = -1;
  for (; isdigit(ch); ch = getchar()) w = (w << 3) + (w << 1) + (ch ^ '0');
  return f * w;
}
void Chkmax(int &fir, int sec) {
  if (sec > fir) fir = sec;
}
void Chkmin(int &fir, int sec) {
  if (sec < fir) fir = sec;
}
namespace SAM {
const int kNode = kN << 1;
int node_num, last, link[kNode], len[kNode], sz[kNode], tr[kNode][26];
int id[kNode], cnt[kN];
void Init() {
  for (int i = 1; i <= lth; ++i) cnt[i] = 0;
  for (int i = 1; i <= node_num; ++i) {
    for (int j = 0; j < 26; ++j) tr[i][j] = 0;
    link[i] = id[i] = sz[i] = 0;
  }
  node_num = last = 1;
}
void Insert(int ch_) {
  int p = last, now_ = last = ++node_num;
  sz[now_] = 1, len[now_] = len[p] + 1;
  for (; p && !tr[p][ch_]; p = link[p]) tr[p][ch_] = now_;
  if (!p) {
    link[now_] = 1;
    return;
  }
  int q = tr[p][ch_];
  if (len[q] == len[p] + 1) {
    link[now_] = q;
    return;
  }
  int newq = ++node_num;
  len[newq] = len[p] + 1;
  link[newq] = link[q];
  link[q] = link[now_] = newq;
  memcpy(tr[newq], tr[q], sizeof(tr[q]));
  for (; p && tr[p][ch_] == q; p = link[p]) tr[p][ch_] = newq;
}
long long Query() {
  for (int i = 1; i <= node_num; ++i) ++cnt[len[i]];
  for (int i = 1; i <= lth; ++i) cnt[i] += cnt[i - 1];
  for (int i = node_num; i >= 1; --i) id[--cnt[len[i]]] = i;
  long long ans = 0;
  for (int i = node_num; i >= 1; --i) {
    sz[link[id[i]]] += sz[id[i]];
    ans += 1ll * sz[id[i]] * sz[id[i]] * (len[id[i]] - len[link[id[i]]]);
  }
  return ans;
}
}  // namespace SAM
int main() {
  int T = read();
  while (T--) {
    SAM::Init();
    scanf("%s", s + 1);
    lth = strlen(s + 1);
    for (int i = 1; i <= lth; ++i) SAM::Insert(s[i] - 'a');
    printf("%lld\n", SAM::Query());
  }
  return 0;
}