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 JAVA):

Solution:
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author Rustam Musin (t.me/musin_acm)
 */
public class Main {
  public static void main(String[] args) {
    InputStream inputStream = System.in;
    OutputStream outputStream = System.out;
    InputReader in = new InputReader(inputStream);
    OutputWriter out = new OutputWriter(outputStream);
    ILozhnieNovostiSlozhnaya solver = new ILozhnieNovostiSlozhnaya();
    solver.solve(1, in, out);
    out.close();
  }

  static class ILozhnieNovostiSlozhnaya {
    public void solve(int testNumber, InputReader in, OutputWriter out) {
      int t = in.readInt();
      while (t-- > 0) {
        char[] s = (in.next() + "$").toCharArray();
        long answer = new ILozhnieNovostiSlozhnaya.Solver(s).solve();
        out.printLine(answer);
      }
    }

    static long sq(long x) {
      return x * x;
    }

    static class Solver {
      int n;
      ILozhnieNovostiSlozhnaya.SuffixArray sa;

      Solver(char[] s) {
        n = s.length;
        sa = new ILozhnieNovostiSlozhnaya.SuffixArray(s);
      }

      long solve() {
        return solve(0, n - 1, -1);
      }

      long solve(int l, int r, int at) {
        int same = l == r ? (n - sa.getValue(l) - 1) : sa.getLCP2(l, r);
        int len = same - at;
        int count = r - l + 1;
        long result = at == -1 ? 0 : len * sq(count);
        if (l == r) return result;
        while (l <= r) {
          int end = sa.findLast(l, same + 1);
          result += solve(l, end, same);
          l = end + 1;
        }
        return result;
      }

    }

    static class SuffixArray {
      int n;
      char[] s;
      int[] order;
      int[] type;
      ILozhnieNovostiSlozhnaya.SparseTable lcpST;

      SuffixArray(char[] s) {
        this.s = s;
        this.n = s.length;
        initSuffixArray();
        initLCP();
//      print();
      }

      void initLCP() {
        int[] lcp = new int[n];
        for (int i = 0, k = 0; i < n - 1; i++) {
          int pi = type[i];
          int j = order[pi - 1];
          while (s[i + k] == s[j + k]) k++;
          lcp[pi] = k;
          if (k > 0) k--;
        }
        lcpST = new ILozhnieNovostiSlozhnaya.SparseTable(lcp);
      }

      int getValue(int pi) {
        return order[pi];
      }

      int compare2(int pi, int pj, int len) {
        int same = getLCP2(pi, pj);
        if (same >= len) return 0;
        int i = getValue(pi);
        int j = getValue(pj);
        return Character.compare(s[i + same], s[j + same]);
      }

      int findLast(int pi, int len) {
        for (int step = 1 << 20; step > 0; step >>= 1) {
          int pj = pi + step;
          if (pj < n && compare2(pi, pj, len) == 0) pi = pj;
        }
        return pi;
      }

      int getLCP2(int pi, int pj) {
        if (pi == pj) return n;
        return lcpST.get(Math.min(pi, pj) + 1, Math.max(pi, pj));
      }

      void countSort(int countSize) {
        int[] count = new int[countSize];
        for (int t : type) count[t]++;
        for (int i = 0, j = 0; i < countSize; i++) {
          int x = count[i];
          count[i] = j;
          j += x;
        }
        int[] order2 = new int[n];
        for (int x : order) order2[count[type[x]]++] = x;
        System.arraycopy(order2, 0, order, 0, n);
      }

      void initSuffixArray() {
        order = new int[n];
        type = new int[n];
        {
          for (int i = 0; i < n; i++) order[i] = i;
          for (int i = 0; i < n; i++) type[i] = s[i];
          countSort(1 << 8);
          type[order[0]] = 0;
          for (int i = 1, j = 0; i < n; i++) {
            if (s[order[i]] != s[order[i - 1]]) j++;
            type[order[i]] = j;
          }
        }
        for (int len = 1; len < n; len <<= 1) {
          for (int i = 0; i < n; i++) order[i] = (order[i] - len + n) % n;
          countSort(n);
          int[] type2 = new int[n];
          type2[order[0]] = 0;
          for (int i = 1, j = 0; i < n; i++) {
            int left1 = type[order[i]];
            int left2 = type[order[i - 1]];
            int right1 = type[(order[i] + len) % n];
            int right2 = type[(order[i - 1] + len) % n];
            if (left1 != left2 || right1 != right2) j++;
            type2[order[i]] = j;
          }
          System.arraycopy(type2, 0, type, 0, n);
        }
      }

    }

    static class SparseTable {
      int[] lenToLog;
      int[][] st;

      SparseTable(int[] a) {
        int n = a.length;
        lenToLog = new int[n + 1];
        for (int i = 1, j = 0; i <= n; i++) {
          if (i == (1 << (j + 1))) j++;
          lenToLog[i] = j;
        }
        int maxLog = lenToLog[n];
        st = new int[n][maxLog + 1];
        for (int i = n - 1; i >= 0; i--) {
          st[i][0] = a[i];
          for (int j = 1; i + (1 << j) <= n; j++) {
            st[i][j] = Math.min(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]);
          }
        }
      }

      int get(int l, int r) {
        int len = r - l + 1;
        int log = lenToLog[len];
        return Math.min(st[l][log], st[r - (1 << log) + 1][log]);
      }

    }

  }

  static class OutputWriter {
    private final PrintWriter writer;

    public OutputWriter(OutputStream outputStream) {
      writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
    }

    public OutputWriter(Writer writer) {
      this.writer = new PrintWriter(writer);
    }

    public void close() {
      writer.close();
    }

    public void printLine(long i) {
      writer.println(i);
    }

  }

  static class InputReader {
    private InputStream stream;
    private byte[] buf = new byte[1024];
    private int curChar;
    private int numChars;
    private InputReader.SpaceCharFilter filter;

    public InputReader(InputStream stream) {
      this.stream = stream;
    }

    public int read() {
      if (numChars == -1) {
        throw new InputMismatchException();
      }
      if (curChar >= numChars) {
        curChar = 0;
        try {
          numChars = stream.read(buf);
        } catch (IOException e) {
          throw new InputMismatchException();
        }
        if (numChars <= 0) {
          return -1;
        }
      }
      return buf[curChar++];
    }

    public int readInt() {
      int c = read();
      while (isSpaceChar(c)) {
        c = read();
      }
      int sgn = 1;
      if (c == '-') {
        sgn = -1;
        c = read();
      }
      int res = 0;
      do {
        if (c < '0' || c > '9') {
          throw new InputMismatchException();
        }
        res *= 10;
        res += c - '0';
        c = read();
      } while (!isSpaceChar(c));
      return res * sgn;
    }

    public String readString() {
      int c = read();
      while (isSpaceChar(c)) {
        c = read();
      }
      StringBuilder res = new StringBuilder();
      do {
        if (Character.isValidCodePoint(c)) {
          res.appendCodePoint(c);
        }
        c = read();
      } while (!isSpaceChar(c));
      return res.toString();
    }

    public boolean isSpaceChar(int c) {
      if (filter != null) {
        return filter.isSpaceChar(c);
      }
      return isWhitespace(c);
    }

    public static boolean isWhitespace(int c) {
      return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
    }

    public String next() {
      return readString();
    }

    public interface SpaceCharFilter {
      public boolean isSpaceChar(int ch);

    }

  }
}