#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;


#define oset tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> // find_by_order(); order_of_key();
#define mod 1000000007
#define ll long long int
#define mk make_pair
#define pb push_back
#define xx first
#define yy second



double pi=3.141592653589793238;
const int M = 1e9+7;
const int Nmax=5005;
const int MM = 1e7+1;
const int N=100005;



int main(){
//ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int T=1;
cin>>T;
while(T--){
int n;
cin>>n;
vector<pair<ll,ll> > v1,v2;
ll ans=0;
set<int> s;
for(int i=1;i<=n;i++)s.insert(i);
for(int i=0;i<n;i++){
    int k,l,r;
    cin>>k>>l>>r;
    ans+=r;
        if(l-r>0)v1.pb(mk(l-r,k));
        else v2.pb(mk(l-r,k));
}
sort(v1.begin(),v1.end());
reverse(v1.begin(),v1.end());
sort(v2.begin(),v2.end());
reverse(v2.begin(),v2.end());
for(int i=0;i<(int)v2.size();i++){
    auto it=s.upper_bound(v2[i].yy);

    if(it!=s.end()){
        s.erase(it);
    }
    else ans+=v2[i].xx;
 }
for(int i=0;i<v1.size();i++){
    auto it=s.upper_bound(v1[i].yy);

     if(it!=s.begin()){
    it--;
    s.erase(it);
    ans+=v1[i].xx;
 }
 else {
    auto it=s.end();
    it--;
    s.erase(it);
 }
 }
cout<<ans<<"\n";
}
return 0;
}
