Under construction

Still being built. Things may change

Salty Fish

Time limit3sMemory limit512 MB

Summary
Pick which cameras to lock so that the apples on nodes seen only by locked cameras, minus the locking costs, is maximized.
Level

Hard10 of 10

Topics
Tree, Dynamic programming
Solved
No attempts yet

Statement

Little Q has an apple tree with nn nodes, labeled 1,2,…,n1, 2, \ldots, n. The root is node 1, and each edge has length one. Node ii has aia_i apples. Each apple sells for one dollar, so selling tt apples earns tt dollars.

Skywalkert, a close friend of Little Q, lost most of his money betting on programming contests. He wants to steal some apples from this tree and sell them to make money.

The security system takes pictures of the nodes once per hour using mm cameras. Let d(x,y)d(x, y) be the number of edges on the shortest path from node xx to node yy, and let p(x,k)={y∣y is in the subtree of x and d(x,y)≤k}p(x, k) = \{y \mid y \text{ is in the subtree of } x \text{ and } d(x, y) \le k\}. Note that x∈p(x,k)x \in p(x, k). The image from the ii-th camera shows all nodes in p(xi,ki)p(x_i, k_i). If the security system detects a change in any of these images, it sounds an alarm, and Little Q catches the thief.

Skywalkert is also a gifted hacker. He can lock some cameras so that their images never change. To lock the ii-th camera, he pays cic_i dollars. Skywalkert pays for all hacks after he steals the apples and sells them.

Write a program that helps Skywalkert earn the most money possible without being caught.

Input

The first line contains TT (1≤T≤10 0001 \le T \le 10\,000), the number of test cases.

Each test case starts with a line containing nn and mm (1≤n,m≤300 0001 \le n, m \le 300\,000).

The next line contains n−1n-1 integers f2,f3,…,fnf_2, f_3, \ldots, f_n (1≤fi<i1 \le f_i < i), the parents of nodes 2,3,…,n2, 3, \ldots, n.

The next line contains nn integers a1,a2,…,ana_1, a_2, \ldots, a_n (1≤ai≤1091 \le a_i \le 10^9), the number of apples on nodes 1,2,…,n1, 2, \ldots, n.

Each of the next mm lines contains three integers xix_i, kik_i, and cic_i (1≤xi≤n1 \le x_i \le n, 0≤ki≤n0 \le k_i \le n, 1≤ci≤1091 \le c_i \le 10^9), the parameters of camera ii.

The sum of nn over all test cases is at most 10610^6, and the sum of mm over all test cases is at most 10610^6.

Output

For each test case, print one line containing an integer: the maximum amount of dollars Skywalkert can earn.

Examples1

  1. Example 1

    Input
    1
    6 3
    1 1 2 2 3
    2 5 4 3 3 2
    2 1 3
    3 1 7
    1 2 4
    
    Expected output
    6