#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define mp make_pair
#define pb push_back
#define ip pair<ll,ll>
#define ff first
#define ss second
#define MAX 100005
const ll MOD = 1000000007;

int main(){
	ios_base::sync_with_stdio(0); cin.tie(0);

	int t;
	cin>>t;
	while(t--){
		ll n;
		cin>>n;
		ll k[n],l[n],r[n];
		
		vector<ip> vl,vr;

		ll ans = 0;
		for(int i=0;i<n;i++){
			cin>>k[i]>>l[i]>>r[i];
			--k[i];
			ans += l[i];
			if(k[i] == n-1)
				continue;
			if(l[i]<r[i])
				vr.pb({r[i]-l[i],k[i]});
			else if(l[i]>r[i]){
				vl.pb({l[i]-r[i],k[i]});
				ans += r[i]-l[i];
			}
		}

		sort(vl.begin(),vl.end());

		set<int>::iterator it;

		set<int> s;
		for(int i=0;i<n;i++)
			s.insert(i);
		for(int i=vl.size()-1;i>=0;i--){
			it = s.upper_bound(vl[i].ss);
			if(it == s.begin())
				continue;
			--it;
			s.erase(it);
			ans += vl[i].ff;
		}

		sort(vr.begin(),vr.end());

		s.clear();
		for(int i=0;i<n;i++)
			s.insert(i);
		for(int i=vr.size()-1;i>=0;i--){
			it = s.lower_bound(vr[i].ss+1);
			if(it == s.end())
				continue;
			s.erase(it);
			ans += vr[i].ff;
		}

		cout<<ans<<endl;
	}				
}
