#include<iostream>
#include<cstring>
#include<algorithm>

using namespace std;

const int N = 200050;
int T, n;
struct camel{
	int k, l ,r;
} camels[N];

int main() {
	scanf("%d", &T);
	while(T--)
	{
		scanf("%d", &n);
		for(int i = 0, k, l, r; i < n; ++i)
		{
			scanf("%d %d %d", &k, &l, &r);
			camels[i] = {k, l, r};
		}
		sort(camels, camels + n, [](auto& x, auto& y) -> bool
				{ return x.r - x.l < y.r - y.l;} );
		int lo = 0, hi = n;
		while(lo < hi)
		{
			int mid = lo + hi >> 1;
			if(camels[mid].r - camels[mid].l >= 0) hi = mid;
			else lo = mid + 1;
		}
		sort(camels, camels + lo, [](auto& x, auto& y) -> bool
				{return (x.k == y.k ? (x.r - x.l < y.r - y.l): x.k < y.k) ;});
		sort(camels + lo, camels + n, [](auto& x, auto& y) -> bool
				{return (x.k == y.k ? (x.r - x.l < y.r - y.l): x.k < y.k) ;});
//		for(int i = 0; i < n; ++i)
//			printf("camels[%d]: k = %d, l = %d, r = %d\n",
//					i, camels[i].k, camels[i].l, camels[i].r);
		long long res = 0;
		int lm = 1, rm = 1;
		for(int i = 0; i < lo; ++i)
			if(camels[i].k >= lm) res += camels[i].l, ++lm;
			else res += camels[i].r;
		for(int i = n - 1; i >= lo; --i)
			if(n - camels[i].k >= rm) res += camels[i].r, ++rm;
			else res += camels[i].l;
		cout << res << '\n';
	}
	return 0;
}
