Tina Town is a friendly place. People there care about each other.
A trie which was planted by the first mayor of Tina Town grows in the center of the town.
We define a palindrome substring in a trie a string that is a suffix of a string which the path from the root to any node represents and the string is a palindrome. Two palindromes are different if and only if their positions are different. Now, Tina wants to know the sum of the length of all palindrome substrings. Tina didn’t know the answer so she asked you to find out the answer for her.
The first line contains a integer – number of cases
For each case, the first line is an integer $N$ representing the number of nodes in trie except the root.
The following $N$ lines contains a letter between $a$ and $d$ – the letter that node $n[i]$ stores and a number $f[i]$ – the index of $n[i]$’s father. It’s guaranteed that $fa[i] \leq i$. If $fa[i] = 0$ n[i] is the root.
$T \leq 10, N \leq 2*{10}^6$
The first line contains a integer – number of cases
For each case, the first line is an integer $N$ representing the number of nodes in trie except the root.
The following $N$ lines contains a letter between $a$ and $d$ – the letter that node $n[i]$ stores and a number $f[i]$ – the index of $n[i]$’s father. It’s guaranteed that $fa[i] \leq i$. If $fa[i] = 0$ n[i] is the root.
$T \leq 10, N \leq 2*{10}^6$