#include<bits/stdc++.h>
using namespace std;

#define IN inline
#define CO const
typedef long long int64;

template<class T> IN T read(){
	T x=0,w=1;char c=getchar();
	for(;!isdigit(c);c=getchar())if(c=='-') w=-w;
	for(;isdigit(c);c=getchar()) x=x*10+c-'0';
	return x*w;
}
template<class T> IN T read(T&x){
	return x=read<T>();
}

CO int N=2e5+10;
int K[N],L[N],R[N];

void real_main(){
	int n=read<int>();
	vector<int> F,G;
	int64 ans=0;
	for(int i=1;i<=n;++i){
		read(K[i]),read(L[i]),read(R[i]);
		ans+=min(L[i],R[i]);
		if(L[i]>R[i]) F.push_back(i);
		else if(L[i]<R[i]) G.push_back(i);
	}
	sort(F.begin(),F.end(),[&](int a,int b)->bool{
		return K[a]<K[b];
	});
	priority_queue<int,vector<int>,greater<int> > heap;
	for(int i:F){
		if((int)heap.size()<K[i]){
			ans+=L[i]-R[i];
			heap.push(L[i]-R[i]);
		}
		else if(heap.size() and heap.top()<L[i]-R[i]){
			ans+=L[i]-R[i]-heap.top();
			heap.pop(),heap.push(L[i]-R[i]);
		}
	}
	sort(G.begin(),G.end(),[&](int a,int b)->bool{
		return n-K[a]<n-K[b];
	});
	while(heap.size()) heap.pop();
	for(int i:G){
		if((int)heap.size()<n-K[i]){
			ans+=R[i]-L[i];
			heap.push(R[i]-L[i]);
		}
		else if(heap.size() and heap.top()<R[i]-L[i]){
			ans+=R[i]-L[i]-heap.top();
			heap.pop(),heap.push(R[i]-L[i]);
		}
	}
	printf("%lld\n",ans);
}
int main(){
	for(int T=read<int>();T--;) real_main();
	return 0;
}
