#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];

  ll presum=0;
  rep(i,N) presum+=min(L[i],R[i]);

  vector<tuple<ll,int,int>> z;
  rep(i,N){
      if(L[i]==R[i]) continue;
      if(L[i]>R[i]) z.push_back(mkt(L[i]-R[i],K[i],1));
      else if(L[i]<R[i]) z.push_back(mkt(R[i]-L[i],K[i],0));
  }
  sort(all(z));
  reverse(all(z));

  int te=3*N;
  int se=0;
  LazySegmentTree<int,int> lseg(N+1,te,se);
  LazySegmentTree<int,int> rseg(N+1,te,se);
  vector<int> init(N+1,0);
  for(int i=0;i<=N;i++) init[i]=i;
  lseg.build(init);
  rseg.build(init);
  vector<ll> dp(N+1,0);
  int num=0;
  for(int i=0;i<z.size();i++){
      ll val=get<0>(z[i]);
      int pos=get<1>(z[i]);
      int fl=get<2>(z[i]);
      int resl=lseg.query(pos,N+1);
      int resr=rseg.query(N-pos,N+1);
      if(fl==1&&resl==0) continue;
      if(fl==0&&resr==0) continue;

      dp[num+1]=dp[num]+val;
      num++;
      if(fl==1) lseg.update(pos,N+1,-1);
      if(fl==0) rseg.update(N-pos,N+1,-1);
  }

  ll ans=0;
  for(int i=0;i<=N;i++) chmax(ans,dp[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: 20812