#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(n);i++)
using namespace std;
using Z=long long;
Z S(vector<pair<int,int>> a){
sort(a.begin(),a.end());Z A;
priority_queue<int,vector<int>,greater<>> Q;
for(auto [k,d]:a){
Q.emplace(d);A+=d;
while(Q.size()>k)A-=Q.top(),Q.pop();
}return A;}
int main(){
	int q; scanf("%d",&q);
	rep(_,q){
		int n; scanf("%d",&n);
		Z sum=0;
		vector<pair<int,int>> L,R;
		rep(i,n){
			int k,l,r; scanf("%d%d%d",&k,&l,&r);
			if     (l>r) L.emplace_back( k ,l-r), sum+=r;
			else if(r>l) R.emplace_back(n-k,r-l), sum+=l;
			else         sum+=l;
		}
		cout<<sum+S(L)+S(R)<<endl;
	}

}
