Replica Placement
Topsky wants to build a content delivery network. It contains an origin server and some mirror servers as shown in figure 1. Original data is put at the origin server (root in figure 1) while replicas are put at some of the mirror servers (node 2 and 5 in figure 1). When a node issues a request of data, it will try to find its destination in following steps.
Check if there is a replica at its own place. If yes, the request meets. Otherwise do step 2.
Forward the request to its parent, and let its parent do step 1.
The cost of meeting the request C(v) is defined as the sum of weight of the edges along the road. If C(v) is not greater than an upper bound Q(v), then the retrieval cost is satisfied. Topsky further assumes a nonnegative cost S(v) which means the cost of storing data at node v. Note that the origin server is special, the cost of storing data at origin server is 0. Now Topsky wants to find the way of replicas placement such the retrieval cost of all nodes are satisfied while the total storage cost is minimal.
Input
The first line contains a single integer t (t ≤ 20), indicating the number of test cases. Each case begins with an integer n (1 ≤ n ≤ 1000) that indicates the number of servers. Then n lines follow. Each line contains four integers F_v (0 ≤ F_v ≤ n), Q_v, S_v and W_v (0 ≤ Q_v, S_v, W_v ≤ 10^5). In line i (1 ≤ i ≤ n), F_v is the father of node i (if F_v is 0, it means node i is the origin server, Q_v is -1, S_v is 0 and W_v is 0), Q_v is upper bound of retrieval cost, S_v is storing cost of node i and W_v is weight of this edge between node i and node F_v.
Output
For each test case, output the minimal storage cost in one line.