#include<bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i,n) for(int i=0;i<n;i++)
#define REP(i,n) for(int i=1;i<n;i++)
#define rev(i,n) for(int i=n-1;i>=0;i--)
#define all(v) v.begin(),v.end()
#define P pair<int,int>
#define len(s) (int)s.size()

template<class T> inline bool chmin(T &a, T b){
	if(a>b){a=b;return true;}
	return false;
}
template<class T> inline bool chmax(T &a, T b){
	if(a<b){a=b;return true;}
	return false;
}
constexpr int mod = 1e9+7;
constexpr int inf = 3e18;

int N;
void solve(){
	cin>>N;
	vector<P>Front,Back;
	int ans=0;
	rep(i,N){
		int K,L,R;cin>>K>>L>>R;
		if(L>=R){
			ans+=L;
			Front.push_back({K-1,L-R});
		}else {
			ans+=R;
			Back.push_back({N-K,R-L});
		}
	}
	sort(all(Front));sort(all(Back));
	priority_queue<int,vector<int>,greater<int>>que;
	int Index=0;
	rep(i,N){
		while(Index<len(Front)&&Front[Index].first<=i)
			que.push(Front[Index++].second);
		while(len(que)>i+1){
			ans-=que.top();que.pop();
		}
	}
	while(len(que))que.pop();
	Index=0;
	rep(i,N){
		while(Index<len(Back)&&Back[Index].first<=i)
			que.push(Back[Index++].second);
		while(len(que)>i){
			ans-=que.top();que.pop();
		}
	}
	cout<<ans<<"\n";
}
signed main(){
	int T;
	cin>>T;
	while(T--)solve();
}
