At the beginning the world had n cities and n−1 roads joining them. The cities carry the integer numbers 0 through n−1. Every road can be walked in both directions and joins two different cities. Starting from any city you could reach every other city by walking along one or more roads.
Humans grew wise and built a bright civilization, but laying a new road between two cities was the one thing they never managed.
The creator watched this and started adding one road between two cities every year. He also wanted to know whether humans were clever enough to use the new roads, so he set them the following puzzle.
Every time one road is added, choose some of the roads laid so far so that all cities are connected to each other, directly or indirectly. Make the sum of the lengths of the chosen roads as small as possible, and report that minimum sum.
You represent humankind, and you have to solve the puzzle.
The first line contains the number of test cases T.
The first line of each test case contains the number of cities n and the number of roads the creator laid m, separated by a space.
The next n−1 lines describe the world at the beginning. Line i (1≤i≤n−1) among them contains the integers ui, ci (0≤ui<i, 0≤ci≤107), separated by a space. This means that city i and city ui are joined by a road of length ci.
The next m lines give the roads the creator laid, in the order he laid them. Line j (1≤j≤m) among them contains the integers uj, vj, cj (0≤uj,vj<n, 0≤cj≤107), separated by a space. This means that the j-th road joins city uj and city vj and has length cj. uj and vj can be equal, and the road can join two cities that a road already joins.
The limits are 1≤T≤10, 1≤n≤2000, and 1≤m≤2000.
Print one line for each test case. On that line print the XOR of all m minimum sums, one for each added road.