#include<algorithm>
#include<stack>
#include<ctime>
#include<cstring>
#include<cmath>
#include<iostream>
#include<iomanip>
#include<cstdio>
#include<queue>
using namespace std;
typedef int _int;
#define int long long 
inline int read(){
	int num=0,f=1;char x=getchar();
	while(x<'0'||x>'9'){if(x=='-')f=-1;x=getchar();}
	while(x>='0'&&x<='9'){num=num*10+x-'0';x=getchar();}
	return num*f;
}
const int maxn=200005;
struct point{
	int k,d;
}p[maxn],a[maxn],b[maxn];
priority_queue<int, vector<int>, greater<int> > pq;
int n,cnt1,cnt2;
bool cmp1(point a,point b){
	return a.k<b.k;
}
bool cmp2(point a,point b){
	return a.k>b.k;
}
_int main(){
	int T=read();
	while(T--){
		n=read();
		int ans=0;cnt1=cnt2=0;
		for(int i=1,l,r;i<=n;++i){
			p[i].k=read();l=read();r=read();
			p[i].d=l-r;
			if(l-r>0){
				ans+=r;
				a[++cnt1]=p[i];
			}
			else {
				ans+=l;
				b[++cnt2]=p[i];
				b[cnt2].d=-b[cnt2].d;
			}
		}
		sort(a+1,a+cnt1+1,cmp1);
		sort(b+1,b+cnt2+1,cmp2);
		for(int i=1;i<=cnt1;++i){
			pq.push(a[i].d);
			if(pq.size()>a[i].k)pq.pop();
		}
		while(pq.size()){ans+=pq.top();pq.pop();}
		for(int i=1;i<=cnt2;++i){
			pq.push(b[i].d);
			if(pq.size()>n-b[i].k)pq.pop();
		}
		while(pq.size()){ans+=pq.top();pq.pop();}
		printf("%lld\n",ans);
	}
	return 0;
}
