#include <bits/stdc++.h>
#define fst first
#define snd second
#define fore(i,a,b) for(int i=a,ThxDem=b;i<ThxDem;++i)
#define pb push_back
#define ALL(s) s.begin(),s.end()
#define FIN ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define SZ(s) int(s.size())
using namespace std;
typedef long long ll;
typedef pair<ll,ll> ii;

const int MAXN=200010;
vector<ll> v[MAXN],v2[MAXN];

int main(){FIN;
	int t; cin>>t;
	while(t--){
		ll n,ans=0; cin>>n;
		fore(i,0,n+1) v[i].clear(),v2[i].clear();
		fore(i,0,n){
			int k,l,r; cin>>k>>l>>r;
			if(l-r>0)	ans+=r,v[k-1].pb(l-r);
			else ans+=l,v2[k].pb(r-l);
		}
		multiset<ll> did;
		fore(i,0,n){
			for(auto x:v[i]){
				if(SZ(did)<i+1) did.insert(x);
				else if(*did.begin()<x) did.erase(did.begin()),did.insert(x);
			}
		}
		for(auto x:did) ans+=x;
		

		did.clear();
		for(int i=n-1;i>=0;i--){
			for(auto x:v2[i]){
				if(SZ(did)<(n-i)) did.insert(x);
				else if(*did.begin()<x) did.erase(did.begin()),did.insert(x);
			}
		}
		for(auto x:did) ans+=x;
		
		cout<<ans<<"\n";
	}
}