#include <bits/stdc++.h>
using namespace std;
typedef int64_t ll;

int main(){
  int t,n;
  ll k,l,r,b;
  pair<ll,ll> a;
  cin>>t;
  for(int i=0;i<t;i++){
    cin>>n;
    ll ans=0;
    priority_queue<pair<ll,ll>,vector<pair<ll,ll>>,greater<pair<ll,ll>>> lq;
    priority_queue<pair<ll,ll>> rq;
    for(int j=0;j<n;j++){
      cin>>k>>l>>r;
      ans+=min(l,r);
      if(l>=r)lq.push({k,l-r});
      else rq.push({k,r-l});
    }
    priority_queue<ll,vector<ll>,greater<ll>> la,ra;
    ll lac=0,rac=0;
    for(int j=1;j<=n;j++){
      if(lq.empty())break;
      a=lq.top();
      while(a.first==j){
        lq.pop();
        la.push(a.second);
        lac++;
        ans+=a.second;
        if(lq.empty())break;
        else a=lq.top();
      }
      while(lac>j){
        b=la.top();
        la.pop();
        ans-=b;
        lac--;
      }
    }
    for(int j=n;j>=1;j--){
      if(rq.empty())break;
      a=rq.top();
      while(a.first==j){
        rq.pop();
        ra.push(a.second);
        rac++;
        ans+=a.second;
        if(rq.empty())break;
        else a=rq.top();
      }
      while(rac>n-j){
        b=ra.top();
        ra.pop();
        ans-=b;
        rac--;
      }
    }
    cout<<ans<<endl;
  }
}
