Pangaea 1

No attempts yetTime limit20sMemory limit256 MB

Problem

At the beginning the world had nn cities and n1n-1 roads joining them. The cities carry the integer numbers 00 through n1n-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.

Input

The first line contains the number of test cases TT.

The first line of each test case contains the number of cities nn and the number of roads the creator laid mm, separated by a space.

The next n1n-1 lines describe the world at the beginning. Line ii (1in11 \le i \le n-1) among them contains the integers uiu_i, cic_i (0ui<i0 \le u_i < i, 0ci1070 \le c_i \le 10^7), separated by a space. This means that city ii and city uiu_i are joined by a road of length cic_i.

The next mm lines give the roads the creator laid, in the order he laid them. Line jj (1jm1 \le j \le m) among them contains the integers uju_j, vjv_j, cjc_j (0uj,vj<n0 \le u_j, v_j < n, 0cj1070 \le c_j \le 10^7), separated by a space. This means that the jj-th road joins city uju_j and city vjv_j and has length cjc_j. uju_j and vjv_j can be equal, and the road can join two cities that a road already joins.

The limits are 1T101 \le T \le 10, 1n20001 \le n \le 2000, and 1m20001 \le m \le 2000.

Output

Print one line for each test case. On that line print the XOR of all mm minimum sums, one for each added road.