Under construction

Still being built. Things may change

String Strange Sum

Time limit4sMemory limit1024 MB

Summary
Given a lowercase string s, sum over all segments [l, r] with 2 <= l <= r <= |s| the longest suffix of s[1, l-1] that splits into prefixes of s[l, r].
Level

Hard8 of 10

Topics
String matching, String, Dynamic programming
Solved
No attempts yet

Problem

You are given a string ss consisting of lowercase English letters.

Consider a segment [ℓ,r][\ell, r] such that 2≤ℓ≤r≤∣s∣2 \leq \ell \leq r \leq |s|. Let f(ℓ,r)f(\ell, r) be the length of the longest suffix of s[1,ℓ−1]s[1, \ell-1] that can be divided into prefixes of s[ℓ,r]s[\ell, r]. If there is no such suffix, then f(ℓ,r)=0f(\ell, r) = 0.

Find the value of

∑ℓ=2∣s∣∑r=ℓ∣s∣f(ℓ,r)\sum_{\ell=2}^{|s|} \sum_{r=\ell}^{|s|} f(\ell, r)

Input

The first line contains a single integer tt (1≤t≤1051 \leq t \leq 10^5), the number of test cases. Each of the next tt lines contains a string ss (2≤∣s∣≤2⋅1052 \leq |s| \leq 2 \cdot 10^5) made of lowercase English letters. The sum of ∣s∣|s| over all test cases does not exceed 2⋅1052 \cdot 10^5.

Output

For each test case, print a single integer, the answer to the problem.

Hint

Consider the third test case. Here f(2,2)=0f(2, 2) = 0, f(2,3)=0f(2, 3) = 0, f(2,4)=0f(2, 4) = 0, f(2,5)=0f(2, 5) = 0, f(3,3)=0f(3, 3) = 0, f(3,4)=2f(3, 4) = 2, f(3,5)=2f(3, 5) = 2, f(4,4)=0f(4, 4) = 0, f(4,5)=2f(4, 5) = 2, and f(5,5)=0f(5, 5) = 0. The answer is 66.

Examples1

  1. Example 1

    Input
    8
    aa
    ab
    ababa
    abaaba
    abacaba
    abaaababaab
    aababcabcbc
    abcdabcabaabcd
    
    Expected output
    1
    0
    6
    7
    0
    74
    51
    20