#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(n);i++)
using namespace std;
using Z=long;
Z A=0,q,n,S=0,k,l,r;
Z S(vector<pair<Z,Z>> a){
sort(a.begin(),a.end());A=0;
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(){cin>>q;
rep(_,q){cin>>n;vector<pair<Z,Z>> L,R;
rep(i,n){cin>>k>>l>>r;
if(l>r)L.emplace_back(k,l-r),S+=r;
else if(r>l) R.emplace_back(n-k,r-l),S+=l;
else S+=l;}
cout<<sum+S(L)+S(R)<<endl;}}
