Problem:
Score : 500 points

Problem Statement

We have N camels numbered 1,2,\ldots,N.
Snuke has decided to make them line up in a row.

The happiness of Camel i will be L_i if it is among the K_i frontmost camels, and R_i otherwise.

Snuke wants to maximize the total happiness of the camels. Find the maximum possible total happiness of the camel.

Solve this problem for each of the T test cases given.

Constraints

All values in input are integers.

1 \leq T \leq 10^5

1 \leq N \leq 2 \times 10^{5}

1 \leq K_i \leq N

1 \leq L_i, R_i \leq 10^9

The sum of values of N in each input file is at most 2 \times 10^5.

Input

Input is given from Standard Input in the following format:

T
\mathrm{case}_1
\vdots
\mathrm{case}_T

Each case is given in the following format:

N
K_1 L_1 R_1
\vdots
K_N L_N R_N

Output

Print T lines. The i-th line should contain the answer to the i-th test case.

Sample Input 1

3
2
1 5 10
2 15 5
3
2 93 78
1 71 59
3 57 96
19
19 23 16
5 90 13
12 85 70
19 67 78
12 16 60
18 48 28
5 4 24
12 97 97
4 57 87
19 91 74
18 100 76
7 86 46
9 100 57
3 76 73
6 84 93
1 6 84
11 75 94
19 15 3
12 11 34

Sample Output 1

25
221
1354

In the first test case, it is optimal to line up the camels in the order 2, 1.

Camel 1 is not the frontmost camel, so its happiness will be 10.

Camel 2 is among the two frontmost camels, so its happiness will be 15.

In the second test case, it is optimal to line up the camels in the order 2, 1, 3.

Camel 1 is among the two frontmost camels, so its happiness will be 93.

Camel 2 is the frontmost camel, so its happiness will be 71.

Camel 3 is among the three frontmost camels, so its happiness will be 57.

Solution in JavaScript (Node.js 12.16.1) (judge verdict: Wrong Answer)

//WA & TLE まだ競合的な場合をあんまり試してない？ あとdpでやる必要があんまりない　jループの途中飛ばせるのでは
function Main(input) {
    input = input.trim().split("\n").map(function(x) { return x.split(" ")});    
    var l = 1;
    const lowerBound = (arr, n) => {
        let first = 0, last = arr.length - 1, middle;
        while (first <= last) {
            middle = 0 | (first + last) / 2;
            if (arr[middle] < n) first = middle + 1;
            else last = middle - 1;
        }
        return first;
    }
    while (true){
        if (input[l] == null) {
            break;
        }
        var N = parseInt(input[l][0], 10);
        var arr_KLR1 = [];  
        var arr_KLR2 = [];  
        var KList1 = [];
        var KList2 = [];
        var line;
        var lowLimit = 0;
        var Nafter = N;
        for (var i = 0; i < N; i++){
            line = input[l + i + 1].map(e => parseInt(e, 10));
            console.log
            if (line[0] === N){
                lowLimit += line[1];
                Nafter --;
            } else if (line[1] > line[2]) {
                arr_KLR1.push(line);
                lowLimit += line[2];
                KList1.push(line[0] - 1);
            } else if (line [1] < line[2]){
                line = [N - line[0], line[2], line[1]];
                arr_KLR2.push(line);
                lowLimit += line[2];
                KList2.push(line[0] - 1);
            } else {
                lowLimit += line[1];
                Nafter --;
            }
        }
        arr_KLR1.sort((a, b) => a[0] - b[0]);
        arr_KLR2.sort((a, b) => a[0] - b[0]);
        KList1 = KList1.sort((a, b) => a - b).filter(function (x, i, self) {
            return self.indexOf(x) === i;
        });
        KList2 = KList2.sort((a, b) => a - b).filter(function (x, i, self) {
            return self.indexOf(x) === i;
        });
        var spaceList1 = [[], []];
        var spaceList2 = [[], []];
        var getSpaceListIndex1 = Array.from({length: KList1[KList1.length - 1] + 1}, () => 0);
        var getSpaceListIndex2 = Array.from({length: KList1[KList2.length - 1] + 1}, () => 0);
        var old = -1;
        for (var i = 0; i < KList1.length; i++){
            spaceList1[0].push(KList1[i] - old);
            spaceList1[1].push(0);
            getSpaceListIndex1[KList1[i]] = i;
            old = KList1[i];
        }
        old = -1;
        for (var i = 0; i < KList2.length; i++){
            spaceList2[0].push(KList2[i] - old);
            spaceList2[1].push(0);
            getSpaceListIndex2[KList2[i]] = i;
            old = KList2[i];
        }



        var ans = lowLimit;
        var bestPlace, tmpSpaceListIndex;
        for (var i = 0; i < arr_KLR1.length; i++){
            increase = arr_KLR1[i][1] - arr_KLR1[i][2];
            tmpSpaceListIndex = getSpaceListIndex1[arr_KLR1[i][0] - 1];
            while (spaceList1[1][tmpSpaceListIndex] === spaceList1[0][tmpSpaceListIndex] && tmpSpaceListIndex >= 0){
                 tmpSpaceListIndex --;
            }
            if (tmpSpaceListIndex >= 0) {
                spaceList1[1][tmpSpaceListIndex] ++;
                ans += increase;
            }
        }

        for (var i = 0; i < arr_KLR2.length; i++){
            increase = arr_KLR2[i][1] - arr_KLR2[i][2];
            tmpSpaceListIndex = getSpaceListIndex2[arr_KLR2[i][0] - 1];
            while (spaceList2[1][tmpSpaceListIndex] === spaceList2[0][tmpSpaceListIndex] && tmpSpaceListIndex >= 0){
                 tmpSpaceListIndex --;
            }
            if (tmpSpaceListIndex >= 0) {
                spaceList2[1][tmpSpaceListIndex] ++;
                ans += increase;
            }
        }

    
        console.log(ans);


        l += N + 1;
    }
}


Main(require("fs").readFileSync("/dev/stdin", "utf8"));