#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=2e5+13;
struct node{
	int val,pos;
	bool operator <(const node &b)
	const{
		return pos<b.pos;//从小到大 
	}
}a[maxn],b[maxn]; 
priority_queue<int,vector<int>,greater<int> > q;//greater默认小根堆
int main()
{
	int t,n;
	scanf("%d",&t);
	while(t--)
	{
		int k,l,r;
		ll ans=0;
		int cnt1=0,cnt2=0;
		scanf("%d",&n);
		for(int i=1;i<=n;i++)
		{
          scanf("%d%d%d",&k,&l,&r);
        if(l>r) ans+=1ll*r,a[++cnt1]=(node){l-r,k};
		else ans+=1ll*l,b[++cnt2]=(node){r-l,n-k}; 
		}
		sort(a+1,a+1+cnt1);
		sort(b+1,b+1+cnt2);
		for(int i=1;i<=cnt1;i++)
		{
			if(q.size()<a[i].pos) q.push(a[i].val);
			else if(q.size()==a[i].pos)
			{
				if(a[i].val>q.top()) q.push(a[i].val),q.pop();
			}
		}
		while(!q.empty()) ans+=1ll*q.top(),q.pop();
		for(int i=1;i<=cnt2;i++)
		{
			if(q.size()<b[i].pos) q.push(b[i].val);
			else if(q.size()==b[i].pos)
			{
				if(b[i].val>q.top()) q.push(b[i].val),q.pop();
			}
		}
		while(!q.empty()) ans+=1ll*q.top(),q.pop();
		printf("%lld\n",ans);
	}    
}