#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define endl '\n'
#define all(x) (x).begin(),(x).end()
const int INF=1e9+5;
const int N=1000+10;
const ll mod=1e9+7;

struct camel{
	int k,l,r;
};

bool cmp(const camel& a,const camel& b){
	return a.k<b.k;
}

ll calc(vector<camel> a){
	ll minSum=0;
	int n=(int)a.size();
	sort(all(a),cmp);
	multiset<int> MS;
	for(int i=0;i<n;++i){
		int pos=a[i].k;
		MS.insert(a[i].l-a[i].r);
		while(pos<(int)MS.size()){
			auto it=MS.begin();
			minSum+=*it;
			MS.erase(it);
		}
	}
	return minSum;
}
		
void solve(){
	int n;
	ll ans=0;
	cin>>n;
	vector<camel> inFront,inBack;
	vector<camel> a(n);
	for(int i=0;i<n;++i){
		cin>>a[i].k>>a[i].l>>a[i].r;
		if(a[i].l>a[i].r){
			ans+=a[i].l;
			inFront.push_back(a[i]);
		}
		else{
			swap(a[i].l,a[i].r);
			ans+=a[i].l;
			a[i].k=n-a[i].k;
			inBack.push_back(a[i]);
		}
	}
	ans-=calc(inFront);
	ans-=calc(inBack);
	cout<<ans<<endl;
}

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	int t;
	cin>>t;
	while(t--) solve();
	return 0;
}
