#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define ll long long
#define fi first
#define se second
#define vi vector<int> 
#define pii pair<int,int>
const int N = 2e5 +5;
int dp[N],n;
int pow(int a,int n,int mod) {
	int y=  1;
	for (;n;n/=2) {
		if (n&1) y = (ll)y*a%mod;
		a = (ll)a*a%mod;
	}
	return y;
}
void solve() {
	cin >> n;
	ll ans = 0;
	vector<pii> v[2];
	for (int i = 0,pos,l,r; i <n; i++) {
		cin >> pos >> l >> r;
		ans += min(l,r);
		if (l >= r) v[0].pb({l-r,pos});
		else v[1].pb({r-l,pos});
	}
	sort(v[0].begin(),v[0].end(),[&](pii a,pii b) {
		if (a.fi != b.fi) return a > b;
		else return a.se < b.se;
	});
	sort(v[1].begin(),v[1].end(),[&](pii a,pii b) {
		if (a.fi != b.fi) return a > b;
		else return a.se > b.se;
	});
	set<int> s;
	for (int i = 1; i <= n; i++) s.insert(i);
	for (auto i : v[0]) {
		int val = i.fi, pos = i.se;
		auto it = s.upper_bound(pos);
		if (it == s.begin()) continue;
		it--;
		s.erase(it);
		ans += i.fi;
	}
	s.clear();
	for (int i = 1; i <= n; i++) s.insert(i);
	for (auto i : v[1]) {
		int val = i.fi, pos = i.se;
		auto it = s.upper_bound(pos);
		if (it == s.end()) continue;
		s.erase(it);
		ans += i.fi;
	}
	cout << ans<<'\n';
}
int main(){
	ios::sync_with_stdio(0);  
	cin.tie(0);
	int t;
	cin >> t;
	while (t--) {
		solve();
	}
	
	
}