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 C (GCC 9.2.1) (judge verdict: Wrong Answer)

#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#include <string.h>

struct rakuda_t {
	int idx;
	int K, L, R;
};

int igutiyuka(const void* x, const void* y) {
	struct rakuda_t a = *(const struct rakuda_t*)x, b = *(const struct rakuda_t*)y;
	return a.K < b.K ? -1 : a.K > b.K;
}

int tomatuharuka(const void* x, const void* y) {
	struct rakuda_t a = *(const struct rakuda_t*)x, b = *(const struct rakuda_t*)y;
	return a.L - a.R > b.L - b.R ? -1 : a.L - a. R < b.L - b.R;
}

int hsize = 0;
struct rakuda_t h[271828];

void ah(int idx) {
	for (;;) {
		int mi = idx;
		int i;
		for (i = 1; i <= 2; i++) {
			int ci = idx * 2 + i;
			if (ci < hsize && h[mi].L - h[mi].R < h[ci].L - h[ci].R) {
				mi = ci;
			}
		}
		if (mi == idx) {
			if (idx == 0) break;
			idx = (idx - 1) / 2;
		} else {
			struct rakuda_t kitaeri = h[idx];
			h[idx] = h[mi];
			h[mi] = kitaeri;
			idx = mi;
		}
	}
}

void hpush(struct rakuda_t r) {
	h[hsize++] = r;
	ah(hsize - 1);
}

struct rakuda_t hpop(void) {
	struct rakuda_t ret;
	if (hsize <= 0) {
		puts("empty!!!!!");
		exit(2);
	}
	ret = h[0];
	h[0] = h[--hsize];
	if (hsize > 0) ah(0);
	return ret;
}

int sumipe(int pos, const struct rakuda_t* r) {
	return pos <= r->K ? r->L : r->R;
}

int maaya(int pos, const struct rakuda_t* r) {
	return pos <= r->K ? r->L - r->R : r->R - r->L;
}


int N;
struct rakuda_t rakuda[271828];

struct rakuda_t usiro[271728];

char tukatta[271828];

int main(void) {
	int T;
	int hayamisaori;
	if (scanf("%d", &T) != 1) return 1;
	for (hayamisaori = 0; hayamisaori < T; hayamisaori++) {
		int i, pos, upos;
		int64_t mamegu = 0;
		if (scanf("%d", &N) != 1) return 1;
		for (i = 0; i < N; i++) {
			if (scanf("%d%d%d", &rakuda[i].K, &rakuda[i].L, &rakuda[i].R) != 3) return 1;
			rakuda[i].idx = i;
		}
		/* TODO: 初期化する!!!!! */
		memset(tukatta, 0, sizeof(*tukatta) * N);

		memcpy(usiro, rakuda, sizeof(*rakuda) * N);
		qsort(rakuda, N, sizeof(*rakuda), igutiyuka);
		qsort(usiro, N, sizeof(*usiro), tomatuharuka);
		pos = 0;
		upos = 0;
		for (i = 1; i <= N; i++) {
			struct rakuda_t asumisu;
			hsize = 0; /* 過去に入れた奴らはておくれ */
			while (pos < N && rakuda[pos].K <= i) {
				if (!tukatta[rakuda[pos].idx]) hpush(rakuda[pos]);
				pos++;
			}
			while (upos < N && tukatta[usiro[upos].idx]) upos++;
			/* ヒープにある && ヒープのやつの方がメリットが大きい */
			if (hsize > 0 && (upos >= N || maaya(i, &h[0]) >= maaya(i, &usiro[upos]))) {
				asumisu = hpop();
				if (tukatta[asumisu.idx]) {
					i--;
					continue;
				}
				tukatta[asumisu.idx] = 1;
				mamegu += sumipe(i, &asumisu);
				/* printf("a %d\n", asumisu.idx); */
			} else {
				if (upos >= N) {
					puts("upos kanbai!!!");
					return 2;
				}
				tukatta[usiro[upos].idx] = 1;
				mamegu += sumipe(i, &usiro[upos]);
				/* printf("b %d\n", usiro[upos].idx); */
				upos++;
			}
		}
		printf("%" PRId64 "\n", mamegu);
	}
	return 0;
}