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.PrintStream;
import java.io.BufferedWriter;
import java.util.PriorityQueue;
import java.util.Collection;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.util.Queue;
import java.util.Comparator;
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) {
      if (false) {
        new ILozhnieNovostiSlozhnaya.Tester().test();
        return;
      }
      int t = in.readInt();
      while (t-- > 0) {
        char[] s = (in.next() + "$").toCharArray();
        long answer = new ILozhnieNovostiSlozhnaya.Solver2(s).solve();
        out.printLine(answer);
      }
    }

    static class Tester {
      void test() {
        for (int len = 1; len <= 10; len++) {
          int total = 1;
          for (int i = 0; i < len; i++) total *= len;
          int max = 0;
          for (int x = 0; x < total; x++) {
            String number = "";
            for (int i = 0, j = x; i < len; i++) {
              number += j % len;
              j /= len;
            }
            int[] lcp = new ILozhnieNovostiSlozhnaya.SuffixArray((number + "$").toCharArray()).lcp;
            int res = 0;
            for (int i = 1; i < lcp.length; i++) res += Math.abs(lcp[i - 1] - lcp[i]);
            max = Math.max(max, res);
          }
          System.err.println(len + " " + max);
        }
      }

    }

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

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

      long solve() {
        int[] lcp = sa.lcp;
        ILozhnieNovostiSlozhnaya.DS2 d = new ILozhnieNovostiSlozhnaya.DS2();
        long answer = 0;
        for (int i = 1; i < lcp.length; i++) {
          d.op(lcp[i]);
          answer += d.totalSum;
        }
        long n = this.n - 1;
        return answer * 2 + n * (n + 1L) / 2;
      }

    }

    static class DS2 {
      long totalSum = 0;
      int ptr = -1;
      Queue<IntIntPair> deltas = new PriorityQueue<>(Comparator.reverseOrder());

      void rem(int upTo) {
        while (!deltas.isEmpty() && deltas.peek().first > upTo) {
          IntIntPair cur = deltas.poll();
          totalSum -= (cur.first + 1L) * cur.second;
          totalSum += (upTo + 1L) * cur.second;
          deltas.add(IntIntPair.makePair(upTo, cur.second));
        }
      }

      void fullAdd() {
        totalSum += ptr + 1;
        deltas.add(IntIntPair.makePair(ptr, 1));
      }

      void compress() {
        while (deltas.size() > 1) {
          IntIntPair cur = deltas.poll();
          if (deltas.peek().first == cur.first) {
            deltas.add(IntIntPair.makePair(cur.first, cur.second + deltas.poll().second));
          } else {
            deltas.add(cur);
            break;
          }
        }
      }

      void op(int len) {
        rem(len - 1);
        ptr = len - 1;
        fullAdd();
        compress();
      }

    }

    static class SuffixArray {
      int n;
      char[] s;
      int[] order;
      int[] type;
      int[] lcp;

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

      void initLCP() {
        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 SparseTable(lcp);
      }

      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 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);

    }

  }

  static class IntIntPair implements Comparable<IntIntPair> {
    public final int first;
    public final int second;

    public static IntIntPair makePair(int first, int second) {
      return new IntIntPair(first, second);
    }

    public IntIntPair(int first, int second) {
      this.first = first;
      this.second = second;
    }

    public boolean equals(Object o) {
      if (this == o) {
        return true;
      }
      if (o == null || getClass() != o.getClass()) {
        return false;
      }

      IntIntPair pair = (IntIntPair) o;

      return first == pair.first && second == pair.second;
    }

    public int hashCode() {
      int result = first;
      result = 31 * result + second;
      return result;
    }

    public String toString() {
      return "(" + first + "," + second + ")";
    }

    public int compareTo(IntIntPair o) {
      int value = Integer.compare(first, o.first);
      if (value != 0) {
        return value;
      }
      return Integer.compare(second, o.second);
    }

  }
}