#include <bits/stdc++.h>
#define MOD 1000000007LL
using namespace std;
typedef long long ll;
typedef pair<int,ll> P;

int n;
int k[200005],l[200005],r[200005];

void solve(){
	scanf("%d",&n);
	ll ans=0;
	vector<P> vf,vb;
	for(int i=0;i<n;i++){
		scanf("%d%d%d",&k[i],&l[i],&r[i]);
		k[i]--;
		ans+=min(l[i],r[i]);
		if(l[i]>=r[i]){
			vf.push_back(P(k[i],l[i]-r[i]));
		}else{
			vb.push_back(P(k[i],r[i]-l[i]));
		}
	}
	sort(vf.begin(),vf.end());
	priority_queue<ll,vector<ll>,greater<ll> > que;
	int cur=0;
	int cnt=0;
	for(int i=0;i<n;i++){
		while(cur<vf.size() && vf[cur].first==i){
			que.push(vf[cur].second);
			cnt++;
			if(cnt>i+1){
				que.pop();
				cnt--;
			}
			cur++;
		}
	}
	while(que.size()){
		ans+=que.top();
		que.pop();
	}
	sort(vb.begin(),vb.end());
	reverse(vb.begin(),vb.end());
	cur=0;
	cnt=0;
	for(int i=n;i>=0;i--){
		while(cur<vb.size() && vb[cur].first==(i-1)){
			que.push(vb[cur].second);
			cnt++;
			if(cnt>(n-i)){
				que.pop();
				cnt--;
			}
			cur++;
		}
	}
	while(que.size()){
		ans+=que.top();
		que.pop();
	}
	printf("%lld\n",ans);
}

int main(void){
	int t;
	scanf("%d",&t);
	for(int i=0;i<t;i++){
		solve();
	}
	return 0;
}
