#include <bits/stdc++.h>
#include <stdlib.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;
typedef vector<double> Vec;
typedef vector<Vec> Mat;
typedef pair<ll,ll> P;
typedef pair<double,ll> Pd;
typedef pair<double,double> PD;
typedef priority_queue<P,vector<P>,greater<P> > P_queue;
typedef priority_queue<Pd,vector<Pd>,greater<Pd> > Pd_queue;

const ll MOD=998244353;
const ll mod=1000000007;
const ll INF=1e15;
const double DEL=1e-6;


#define REP(i,a,b) for(int i=(int)a;i<(int)b;i++)
#define rep(i,n) REP(i,0,n)
#define pb push_back
#define mp make_pair
#define ALL(a) a.begin(),a.end()
#define SORT(a) sort(ALL(a))
#define U_ERASE(V) V.erase(unique(ALL(V)), V.end());
#define ADD(a,b) a=(a+b)%mod
#define MUL(a,b) a=(a*b)%mod

ll Solve(){
    ll N; cin>>N;
    ll ans=0;
    vector<P> A,B;
    rep(i,N){
        ll K,L,R; cin>>K>>L>>R; 
        ans+=min(L,R);
        if(L>R) A.pb(mp(K,L-R));
        else B.pb(mp(N-K,R-L));
    }
    ll a=A.size(), b=B.size();
    //cout<<a<<':'<<b<<endl;
    priority_queue<ll> Q,R;
    sort(ALL(A)); sort(ALL(B));
    reverse(ALL(A)); reverse(ALL(B));
    ll c=0;
    for(int i=a;i>0;i--){
        while(c<a){
            if(A[c].first<i) break; 
            Q.push(A[c].second);
            c++;
            if(c==a) break;
        }
        if(!Q.empty()){
            ans+=Q.top();
            Q.pop();
        }
    }
    c=0;
    for(int i=b;i>0;i--){
        while(c<b){
            if(B[c].first<i) break; 
            R.push(B[c].second);
            c++;
            if(c==b) break;
        }
        if(!RLIMIT_AS.empty()){
            ans+=R.top();
            R.pop();
        }
    }
    return ans;
}

int main(){
    ll T; cin>>T;
    vec a(T);
    rep(i,T) a[i]=Solve();
    rep(i,T) cout<<a[i]<<endl;
}