#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define ll long long
#define ld long double
#define pii pair<int,int>
#define fi first
#define se second
#define mex 1000005
#define fast  ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
//vector<int> adj[mex];
//a+b=a^b+2*(a&b)

// FIRST THINK, THEN CODE
// DO NOT JUMP BETWEEN PROBLEMS

int main(){
	fast
	int t;
	cin>>t;
	while(t--){
		int n;
		cin>>n;
		int k[n],l[n],r[n];
		for(int i=0;i<n;i++)
		 cin>>k[i]>>l[i]>>r[i];
		
		priority_queue<pair<ll,ll>,vector<pair<ll,ll>>,greater<pair<ll,ll>> > p1,p2;
		ll s=0;
		vector<pair<ll,ll>> v1,v2;
		for(int i=0;i<n;i++){
			if(l[i]>=r[i]){
				s=s+r[i];
				v1.push_back({k[i],l[i]-r[i]});
			}
		}
		sort(v1.begin(),v1.end());
		for(int i=0;i<v1.size();i++){
			p1.push({v1[i].se,v1[i].fi});
			if(p1.size()>v1[i].fi)
			 p1.pop();
		}
		while(!p1.empty()){
			s=s+p1.top().fi;
			//cout<<(p1.top().fi)<<endl;
			p1.pop();
		}
		//cout<<s<<endl;
		for(int i=0;i<n;i++){
			if(r[i]>l[i]){
				s=s+l[i];
				v2.push_back({k[i],r[i]-l[i]});
			}
		}
		sort(v2.rbegin(),v2.rend());
		for(int i=0;i<v2.size();i++){
			p2.push({v2[i].se,v2[i].fi});
			if(p2.size()>n-v2[i].fi)
			 p2.pop();
		 }
		 while(!p2.empty()){
			 s=s+p2.top().fi;
			 p2.pop();
		 }
		 cout<<s<<endl;
	 }
			 
}
