#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int,int> pi;
#define f first
#define s second
#define FAST ios_base::sync_with_stdio(0); cin.tie(0);
const int maxn = 10010;

int n;


void solve() {
	cin >> n;
	
	vector <int> infrontbetter[n+2];
	vector <int> behindbetter[n+2];
	
	int ans = 0;
	
	for (int i =1;i<=n;i++) {
		int k, l, r; cin >> k >> l >> r;
		if (l >= r) infrontbetter[k].push_back(l - r);
		else behindbetter[k].push_back(r - l);
			
		ans += min(l,r);
	}
	//cout << "ANS: " << ans << "\n";
	
	multiset <int> S;
	for (int i = 1; i<= n;i++) {
		for (auto j : infrontbetter[i]) {
			S.insert(j);
		}
		while ((int) S.size() > i) S.erase(S.begin());
	}
	
	for (auto i: S) ans += i;
	//cout << "ANS: " << ans << "\n";
	
	S.clear();
	for (int i = n; i >= 2; i--) {
		for (auto j: behindbetter[i-1]) {
			S.insert(j);
		}
		//for (auto j : S) cout << j << "\n";
		while ((int) S.size() > (n - i + 1)) S.erase(S.begin());
	}
	
	for (auto i: S) ans += i;
	//cout << "ANS: " << ans << "\n";
	cout << ans << "\n";
	
}


int32_t main() {
	FAST
	int tc; cin >> tc;
	while (tc--) solve();

}


