#pragma GCC optimize ("Ofast")
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i=(a); i<(b); i++)
#define FORD(i, a, b) for (int i=(a); i>(b); i--)
#define PPC(x) __builtin_popcount(x)
#define MSB(x) (31 - __builtin_clz(x))
#define SZ(x) ((int)(x).size())
#define pb push_back
#define ALL(x) (x).begin(), (x).end()
#define ithBit(m, i) ((m) >> (i) & 1)
#define ft first
#define sd second
#define kw(a) ((a) * (a))
#ifdef DEBUG
#include "debug.h"
#else
#define dbg(...) 0
#endif
using namespace std;

const int maxN = 1 << 19;

long long f(int n, vector <pair <int, int> > T)
{
	sort(ALL(T));
	reverse(ALL(T));
	set <int> S;
	FOR(i, 0, n)
		S.insert(i);
	long long ret = 0;
	for (auto [dx, i] : T)
	{
		auto it = S.lower_bound(i);
		if (it != S.begin())
			S.erase(prev(it)), ret += dx;
	}
	return ret;
}

void solve()
{
	int n;
	scanf ("%d", &n);
	vector <pair <int, int> > front, back;
	long long res = 0;
	FOR(i, 0, n)
	{
		int k, l, r;
		scanf("%d%d%d", &k, &l, &r);
		
		if (k == n)
		{
			res += l;
			continue;
		}
		res += min(l, r);
		
		if (l > r)
			front.pb({l - r, k});
		if (r > l)
			back.pb({r - l, n - k});
	}
	
	res += f(n, front);
	res += f(n, back);
	printf("%lld\n", res);
}

int main()
{		
	int t = 1;
	scanf ("%d", &t);
	while (t--)
		solve();
	return 0;
}
