#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cmath>
#include <map>
#include <queue>
#include <iomanip>
#include <set>
#include <tuple>
#define mkp make_pair
#define mkt make_tuple
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define all(v) v.begin(),v.end()
using namespace std;
typedef long long ll;
const ll MOD=1e9+7;
template<class T> void chmin(T &a,const T &b){if(a>b) a=b;}
template<class T> void chmax(T &a,const T &b){if(a<b) a=b;}

#include <functional>

template< typename T, typename S >
struct LazySegmentTree{
    int n;
    vector<T> data;
    vector<S> lazy;
    T te;
    S se;

    inline void merge_functions(S& lazy,S& val){
        lazy+=val;
        //lazy=val;
    }
    inline void operate(T& data,S& val,int len){
        //data+=val*len;
        //data=val*len;
        data+=val;
        //data=val;
    }
    inline T merge_values(T& x,T& y){
        //return x+y;
        return min(x,y);
        //return max(x,y);
    }

    LazySegmentTree(){}
    LazySegmentTree(int sz,T te,S se):te(te),se(se){
        n=1;
        while(n<sz) n*=2;
        data.resize(2*n-1,te);
        lazy.resize(2*n-1,se);
    }

    void build(vector<T> &A){
        for(int k=0;k<int(A.size());k++) data[k+n-1]=A[k];
        for(int k=n-2;k>=0;k--) data[k]=merge_values(data[2*k+1],data[2*k+2]);
    }

    void eval(int k,int l,int r){
        if(lazy[k]==se) return;
        operate(data[k],lazy[k],r-l);
        if(r-l>1){
            merge_functions(lazy[2*k+1],lazy[k]);
            merge_functions(lazy[2*k+2],lazy[k]);
        }
        lazy[k]=se;
    }

    void update(int a,int b,S val,int k=0,int l=0,int r=-1){
        if(r<0) r=n;
        eval(k,l,r);
        if(b<=l||r<=a) return;
        if(a<=l&&r<=b){
            merge_functions(lazy[k],val);
            eval(k,l,r);
        }else{
            update(a,b,val,2*k+1,l,(l+r)/2);
            update(a,b,val,2*k+2,(l+r)/2,r);
            data[k]=merge_values(data[2*k+1],data[2*k+2]);
        }
    }

    T query(int a,int b,int k=0,int l=0,int r=-1){
        if(r<0) r=n;
        eval(k,l,r);
        if(b<=l||r<=a) return te;
        if(a<=l&&r<=b) return data[k];

        T vl=query(a,b,2*k+1,l,(l+r)/2);
        T vr=query(a,b,2*k+2,(l+r)/2,r);
        return merge_values(vl,vr);
    }
};

/*
  LazySegmentTree<ll,ll> seg(N,te,se);
  vector<ll> init(N,0);
  seg.build(init);
*/

void solve(){
  int N;
  cin>>N;
  vector<int> K(N);
  vector<ll> L(N),R(N);
  rep(i,N) cin>>K[i]>>L[i]>>R[i];

  vector<pair<int,ll>> u,v;
  ll presum=0;
  rep(i,N){
    presum+=min(L[i],R[i]);
    if(L[i]==R[i]) continue;
    if(L[i]<R[i]){
      if(K[i]==N) continue;
      v.push_back(mkp(R[i]-L[i],N-K[i]));
    }else u.push_back(mkp(L[i]-R[i],K[i]));
  }
  sort(all(v));
  sort(all(u));
  reverse(all(v));
  reverse(all(u));

  vector<vector<ll>> dp(2,vector<ll> (N+1,0));
  {
    int te=3*N;
    int se=0;
    LazySegmentTree<int,int> seg(N+1,te,se);
    vector<int> init(N+1,0);
    for(int i=0;i<=N;i++) init[i]=i;
    seg.build(init);
    int num=0;
    for(int i=0;i<u.size();i++){
        int pos=u[i].second;
        ll val=u[i].first;
        int res=seg.query(pos,N+1);
        if(res==0) continue;

        dp[0][num+1]=dp[0][num]+val;
        num++;
        seg.update(pos,N+1,-1);
    }
    for(int i=1;i<=N;i++) chmax(dp[0][i],dp[0][i-1]);
  }

  {
    int te=3*N;
    int se=0;
    LazySegmentTree<int,int> seg(N+1,te,se);
    vector<int> init(N+1,0);
    for(int i=0;i<=N;i++) init[i]=i;
    seg.build(init);
    int num=0;
    for(int i=0;i<v.size();i++){
        int pos=v[i].second;
        ll val=v[i].first;
        int res=seg.query(pos,N+1);
        if(res==0) continue;

        dp[1][num+1]=dp[1][num]+val;
        num++;
        seg.update(pos,N+1,-1);
    }
    for(int i=1;i<=N;i++) chmax(dp[1][i],dp[1][i-1]);
    reverse(all(dp[1]));
  }

  ll ans=0;
  for(int i=0;i<=N;i++) chmax(ans,dp[0][i]+dp[1][i]);
  ans+=presum;
  cout<<ans<<"\n";
}

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);

  int T;
  cin>>T;
  rep(i,T) solve();

  return 0;
}


Measured memory_bytes: 18376