#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
#define rep(i,n) for(int i=0;i<n;i++)
#define all(x) x.begin(),x.end()
#define chmin(a,b) a=min(a,b)
#define chmax(a,b) a=max(a,b)
#define N 200010
ll n,l[N],r[N],k[N];
vector<ll> v1[N],v2[N];
void solve(){
	cin>>n;
	rep(i,n+1){
		v1[i].clear();
		v2[i].clear();
	}
	ll base=0;
	ll cnt1=0,cnt2=0;
	rep(i,n){
		cin>>k[i]>>l[i]>>r[i];
		if(l[i]>=r[i]){
			v1[k[i]-1].push_back(l[i]-r[i]);
			base+=r[i];
			cnt1++;
		}
		else{
			v2[k[i]].push_back(r[i]-l[i]);
			base+=l[i];
			cnt2++;
		}
	}
	priority_queue<ll> Q;
	for(ll i=n-1;i>=0;i--){
		for(auto v:v1[i])Q.push(v);
		if(i<cnt1){
			if(!Q.empty()){
				base+=Q.top();
				Q.pop();
			}
		}
	}
	while(!Q.empty())Q.pop();
	for(ll i=0;i<n;i++){
		for(auto v:v2[i])Q.push(v);
		if(cnt1<=i){
			if(!Q.empty()){
				base+=Q.top();
				Q.pop();
			}
		}
	}
	cout<<base<<"\n";
}
int main(){
	cin.tie(0);
	ios::sync_with_stdio(0);
	ll t;
	cin>>t;
	while(t--){
		solve();
	}
}
