#include<bits/stdc++.h>
using namespace std;
int read() {
	int x; scanf("%d", &x); return x;	
}
const int maxn = 3e5 + 10;
const int INF = 1e9;
int mod = 1e9 + 7;
using namespace std;

#define ll long long 
int n, a[maxn], b[maxn];
ll ans;
bool cmp(pair<int,int> a, pair<int,int> b) {
	return a.first == b.first ? a.second > b.second : a.first > b.first;	
}
void Solve(vector<pair<int,int> >&a) {
	set<int>X;
	for (int i = 1; i <= n; ++i) X.insert(i);
	for (int i = 0; i < a.size(); ++i) {
		pair<int,int>u = a[i];
		set<int>::iterator It = X.upper_bound(u.second);
		if (It != X.begin()) {
			X.erase(--It);
			ans += u.first;
		}
	}
}
int main() {
	#ifdef LOCAL
		freopen("1.in", "r", stdin);
	#endif
	int T = read(); 
	while (T--) {
		vector<pair<int,int> >L, R;
		n = read(); ans = 0;
		for (int i = 1; i <= n; ++i) {
			int k = read(), l = read(), r = read();
			ans += min(l, r);
			if (l > r) L.push_back(make_pair(l - r, k));
			else R.push_back(make_pair(r - l, n - k));
		}
		sort(L.begin(), L.end(), cmp);
		sort(R.begin(), R.end(), cmp);
		Solve(L); Solve(R);
		cout << ans << endl;
	}

	return 0;	
}
