#include<bits/stdc++.h>
using namespace std;
const int maxn=200009;

int Tn;

int n;
int K[maxn],L[maxn],R[maxn];
vector<int>G[maxn];

priority_queue<int>q;

int main(){
	scanf("%d",&Tn);
	while(Tn--){
		scanf("%d",&n);
		for(int i=1;i<=n;++i)scanf("%d%d%d",&K[i],&L[i],&R[i]);
		
		long long ans=0;
		
		for(int i=1;i<=n;++i)if(L[i]==R[i])ans+=L[i];
		
		int p=0;
		while(!q.empty())q.pop();
		for(int i=1;i<=n;++i)G[i].clear(); 
		for(int i=1;i<=n;++i){
			if(L[i]>R[i]){
				++p;G[K[i]].push_back(L[i]-R[i]);ans+=R[i];
			}
		}
		for(int i=p+1;i<=n;++i){
			for(int j=0;j<G[i].size();++j){
				int x=G[i][j];
				q.push(x);
			}
		}
		for(int i=p;i>=1;--i){
			for(int j=0;j<G[i].size();++j){
				int x=G[i][j];
				q.push(x);
			}
			if(!q.empty()){
				ans+=q.top();q.pop();
			}
		}
		
		p=0;
		while(!q.empty())q.pop();
		for(int i=1;i<=n;++i)G[i].clear();
		for(int i=1;i<=n;++i){
			if(L[i]<R[i]){
				++p;G[K[i]+1].push_back(R[i]-L[i]);ans+=L[i];
			}
		}
		for(int i=1;i<n-p+1;++i){
			for(int j=0;j<G[i].size();++j){
				int x=G[i][j];
				q.push(x);
			}
		}
		for(int i=n-p+1;i<=n;++i){
			for(int j=0;j<G[i].size();++j){
				int x=G[i][j];
				q.push(x);
			}
			if(!q.empty()){
				ans+=q.top();q.pop();
			}
		}
		printf("%lld\n",ans);
	}
	return 0;
}