Palindrome Partition
A substring is a continuous and non-empty segment of letters from a given string, without any reorders.An even palindrome is a string that reads the same backward as forward and has an even length. For example, strings "zz", "abba", "abccba" are even palindromes, but strings "codeforces", "reality", "aba", "c" are not.A beautiful string is an even palindrome or a string that can be partitioned into some smaller even palindromes.You are given a string $$$s$$$, consisting of $$$n$$$ lowercase Latin letters. Count the number of beautiful substrings of $$$s$$$.
In the first test case, the beautiful substrings are "abaaba", "baab", "aa".In the last test case, the beautiful substrings are "aa" (counted twice), "abba", "bb", "bbaa", "abbaaa".
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). The description of the test cases follows.The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 5\cdot 10^5$$$).The second line of each test case contains a string $$$s$$$. String $$$s$$$ consists of only lowercase Latin letters and has a length of $$$n$$$.It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$5\cdot 10^5$$$.
For each test case print the number of beautiful substrings.
import io, os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

def manacher(s):
    L = ['*']
    n = len(s)
    for i in range(n):
        L.append(s[i])
        L.append('*')
    n = 2 * n + 1
    j = 0
    ini = 1
    final = [0] * n
    while j < n:
        for k in range(ini, min(j + 1, n - j)):
            if L[j - k] == L[j + k]:
                final[j] += 1
            else:
                break
        if final[j] != 0:
            for kk in range(1, final[j] + 1):
                if final[j - kk] < final[j] - kk:
                    final[j + kk] = final[j - kk]
                else:
                    break
            final[j + kk] = final[j] - kk
            ini = final[j] - kk + 1
            j += kk
        else:
            j += 1
            ini = 1
    return final

def t():
    n = int(input())
    s = input()
    X = manacher(s)
    dp1 = [0] * n
    st = []
    for i in range(n - 1, -1, -1):
        while st and st[-1] - X[2 * st[-1]] // 2 > i:
            del st[-1]
        if st:
            dp1[i] = 2 * (st[-1] - i)
        st.append(i)
    dp2 = [0] * (n + 1)
    for j in range(n - 1, -1, -1):
        if dp1[j] != 0:
            dp2[j] = dp2[j + dp1[j]] + 1
    print(sum(dp2))
if __name__ == '__main__':
    for _ in range(int(input())):
        t()
import io, os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
import sys

def manacher(s):
    L = ['*']
    n = len(s)
    for i in range(n):
        L.append(s[i])
        L.append('*')
    n = 2 * n + 1
    j = 0
    ini = 1
    final = [0] * n
    while j < n:
        for k in range(ini, min(j + 1, n - j)):
            if L[j - k] == L[j + k]:
                final[j] += 1
            else:
                break
        if final[j] != 0:
            for kk in range(1, final[j] + 1):
                if final[j - kk] < final[j] - kk:
                    final[j + kk] = final[j - kk]
                else:
                    break
            final[j + kk] = final[j] - kk
            ini = final[j] - kk + 1
            j += kk
        else:
            j += 1
            ini = 1
    return final

def t():
    n = int(input())
    s = input()
    X = manacher(s)
    dp1 = [0] * n
    st = []
    for i in range(n - 1, -1, -1):
        while st and st[-1] - X[2 * st[-1]] // 2 > i:
            del st[-1]
        if st:
            dp1[i] = 2 * (st[-1] - i)
        st.append(i)
    dp2 = [0] * (n + 1)
    for j in range(n - 1, -1, -1):
        if dp1[j] != 0:
            dp2[j] = dp2[j + dp1[j]] + 1
    sys.stdout.write(str(sum(dp2)) + '\n')
if __name__ == '__main__':
    for _ in range(int(input())):
        t()
import io, os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
import sys

def manacher(s):
    L = ['*']
    n = len(s)
    for i in range(n):
        L.append(s[i])
        L.append('*')
    n = 2 * n + 1
    j = 0
    ini = 1
    final = [0] * n
    while j < n:
        for k in range(ini, min(j + 1, n - j)):
            if L[j - k] == L[j + k]:
                final[j] += 1
            else:
                break
        if final[j] != 0:
            for kk in range(1, final[j] + 1):
                if final[j - kk] < final[j] - kk:
                    final[j + kk] = final[j - kk]
                else:
                    break
            final[j + kk] = final[j] - kk
            ini = final[j] - kk + 1
            j += kk
        else:
            j += 1
            ini = 1
    return final

def t():
    n = int(input())
    s = input()
    X = manacher(s)
    dp1 = [0] * n
    st = []
    for i in range(n - 1, -1, -1):
        while st and st[-1] - X[2 * st[-1]] // 2 > i:
            del st[-1]
        if st:
            dp1[i] = 2 * (st[-1] - i)
        st.append(i)
    dp2 = [0] * (n + 1)
    for j in range(n - 1, -1, -1):
        if dp1[j] != 0:
            dp2[j] = dp2[j + dp1[j]] + 1
    sys.stdout.write(str(sum(dp2)) + '\n')
if __name__ == '__main__':
    for _ in range(int(input())):
        t()