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;
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 height[MAXN];
void getHeight(int n) {
  for (int i = 0; i < n; ++i) _rank[pos[i]] = i;
  height[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++;
      height[_rank[i]] = h;
      if (h > 0) h--;
    }
  }
}
struct node {
  int fix;
  int add;
  long long int sum, sq;
};
node ST[5 * MAXN];
void update(int n, int b, int e, int l, int r, int val) {
  if (b == l && e == r) {
    ST[n].fix = val;
    ST[n].add = 0;
    ST[n].sum = (e - b + 1) * 1LL * val;
    ST[n].sq = (e - b + 1) * 1LL * val * 1LL * val;
    return;
  }
  int m = (b + e) >> 1;
  if (r <= m) {
    update(2 * n, b, m, l, r, val);
  } else if (l > m) {
    update(2 * n + 1, m + 1, e, l, r, val);
  } else {
    update(2 * n, b, m, l, m, val);
    update(2 * n + 1, m + 1, e, m + 1, r, val);
  }
  ST[n].fix = -1;
  ST[n].add = 0;
  ST[n].sum = ST[2 * n].sum + ST[2 * n + 1].sum;
  ST[n].sq = ST[2 * n].sq + ST[2 * n + 1].sq;
}
void inc(int n, int b, int e, int l, int r, int val) {
  if (b == l && e == r) {
    ST[n].sq += (e - b + 1) * 1LL * val * 1LL * val + val * 2LL * ST[n].sum;
    ST[n].sum += (e - b + 1) * 1LL * val;
    ST[n].add += val;
    return;
  }
  int m = (b + e) >> 1;
  if (ST[n].fix != -1) {
    update(2 * n, b, m, b, m, ST[n].fix);
    update(2 * n + 1, m + 1, e, m + 1, e, ST[n].fix);
    ST[n].fix = -1;
  }
  if (ST[n].add) {
    inc(2 * n, b, m, b, m, ST[n].add);
    inc(2 * n + 1, m + 1, e, m + 1, e, ST[n].add);
    ST[n].add = 0;
  }
  if (r <= m) {
    inc(2 * n, b, m, l, r, val);
  } else if (l > m) {
    inc(2 * n + 1, m + 1, e, l, r, val);
  } else {
    inc(2 * n, b, m, l, m, val);
    inc(2 * n + 1, m + 1, e, m + 1, r, val);
  }
  ST[n].sum = ST[2 * n].sum + ST[2 * n + 1].sum;
  ST[n].sq = ST[2 * n].sq + ST[2 * n + 1].sq;
}
long long int query(int n, int b, int e, int l, int r) {
  if (b == l && e == r) {
    return ST[n].sq;
  }
  int m = (b + e) >> 1;
  if (ST[n].fix != -1) {
    update(2 * n, b, m, b, m, ST[n].fix);
    update(2 * n + 1, m + 1, e, m + 1, e, ST[n].fix);
    ST[n].fix = -1;
  }
  if (ST[n].add) {
    inc(2 * n, b, m, b, m, ST[n].add);
    inc(2 * n + 1, m + 1, e, m + 1, e, ST[n].add);
    ST[n].add = 0;
  }
  if (r <= m) {
    return query(2 * n, b, m, l, r);
  } else if (l > m) {
    return query(2 * n + 1, m + 1, e, l, r);
  }
  return query(2 * n, b, m, l, m) + query(2 * n + 1, m + 1, e, m + 1, r);
}
void printTree(int id) {
  for (int i = 1; i < id + 1; i++) {
  }
}
string s;
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    cin >> s;
    int n = s.size();
    for (int i = 0; i < n; i++) str[i] = s[i];
    suffixSort(n);
    getHeight(n);
    long long int ans = 0;
    for (int i = 1; i < 5 * n; i++) {
      ST[i].fix = -1;
      ST[i].sum = 0;
      ST[i].sq = 0;
      ST[i].add = 0;
    }
    inc(1, 1, n, 1, n - pos[0], 1);
    for (int i = 1; i < n; i++) {
      int l = height[i];
      ans += query(1, 1, n, l + 1, n);
      update(1, 1, n, l + 1, n, 0);
      inc(1, 1, n, 1, n - pos[i], 1);
    }
    ans += query(1, 1, n, 1, n);
    cout << ans << "\n";
  }
  return 0;
}
