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: Accepted)

#include<stdio.h>
long long int k[200005], l[200005], r[200005];
long long int h[200005], hl;
int comp_h(long long int a, long long int b, int z)
{
	if (z == 0)
	{
		if (l[h[a]] == r[h[a]])
			return 1;
		else if (l[h[b]] == r[h[b]])
			return -1;
		else if (l[h[a]] > r[h[a]] && l[h[b]] > r[h[b]])
		{
			if (k[h[a]] > k[h[b]])
				return 1;
			return -1;
		}
		else if (l[h[a]] < r[h[a]] && l[h[b]] < r[h[b]])
		{
			if (k[h[a]] < k[h[b]])
				return 1;
			return -1;
		}
		else
		{
			if (l[h[a]] < r[h[a]])
				return 1;
			return -1;
		}
	}
	else if (z == 1)
	{
		if (l[h[a]] - r[h[a]] > l[h[b]] - r[h[b]])
			return 1;
		return -1;
	}
	else
	{
		if (r[h[a]] - l[h[a]] > r[h[b]] - l[h[b]])
			return 1;
		return -1;
	}
}
void swap_h(long long int a, long long int b)
{
	long long int f = h[a];
	h[a] = h[b];
	h[b] = f;
	return;
}
void f1(long long int ne, int z)
{
	h[hl] = ne;
	long long int p = hl;
	hl++;
	while (p > 0)
	{
		if (comp_h((p - 1) / 2, p, z) > 0)
			swap_h((p - 1) / 2, p);
		p = (p - 1) / 2;
	}
	return;
}
long long int f2(int z)
{
	hl--;
	swap_h(0, hl);
	long long int p = 0;
	for (;;)
	{
		if (2 * p + 2 < hl)
		{
			if (comp_h(2 * p + 1, 2 * p + 2, z) > 0)
			{
				if (comp_h(p, 2 * p + 2, z) > 0)
					swap_h(p, 2 * p + 2);
				p = 2 * p + 2;
			}
			else
			{
				if (comp_h(p, 2 * p + 1, z) > 0)
					swap_h(p, 2 * p + 1);
				p = 2 * p + 1;
			}
		}
		else if (2 * p + 1 < hl)
		{
			if (comp_h(p, 2 * p + 1, z) > 0)
				swap_h(p, 2 * p + 1);
			p = 2 * p + 1;
		}
		else
			break;
	}
	return h[hl];
}
int main()
{
	int t;
	scanf("%d", &t);
	long long int n;
	long long int i, j, jj;
	long long int c[200005];
	long long int ans;
	long long int p;
	for (; t > 0; t--)
	{
		scanf("%lld", &n);
		for (i = 0; i < n; i++)
			scanf("%lld %lld %lld", &k[i], &l[i], &r[i]);
		hl = 0;
		for (i = 0; i < n; i++)
			f1(i, 0);
		for (i = 0; i < n; i++)
			c[i] = f2(0);
		ans = 0;
		j = 0;
		while (j<n && l[c[j]]>r[c[j]])
			j++;
		hl = 0;
		for (i = 0; i < j; i++)
		{
			ans += l[c[i]];
			f1(c[i], 1);
			if (hl > k[c[i]])
			{
				p = f2(1);
				ans -= l[p];
				ans += r[p];
			}
		}
		jj = j;
		while (jj < n && l[c[jj]] < r[c[jj]])
			jj++;
		hl = 0;
		for (i = j; i < jj; i++)
		{
			ans += r[c[i]];
			f1(c[i], 2);
			if (hl > n - k[c[i]])
			{
				p = f2(2);
				ans -= r[p];
				ans += l[p];
			}
		}
		for (i = jj; i < n; i++)
			ans += l[c[i]];
		printf("%lld\n", ans);
	}
	return 0;
}