#include<iostream>
#include<set>
#include<algorithm>
#include<vector>
using namespace std;
typedef long long li;
#define rep(i,n) for(int i=0;i<(n);i++)
#define df 0
template<class T> void print(const T& t){ cout << t << "\n"; }
template<class T, class... Ts> void print(const T& t, const Ts&... ts) { cout << t; if (sizeof...(ts)) cout << " "; print(ts...); }

li calc(vector<pair<int,li>>& vec){
  int n=vec.size();
  if(n==0) return 0;
  rep(i,n) vec[i].first*=-1;
  sort(vec.begin(),vec.end());
  rep(i,n) vec[i].first*=-1;
  li s=0;
  int i=0,i0=vec[0].first;
  multiset<li> st;
  while(i0){
    while(i<n and vec[i].first==i0) st.insert(-vec[i++].second);
    if(st.size()){
      s-=*st.begin();
      st.erase(st.begin());
    }
    i0--;
  }
  return s;
}

void solve(vector<int>& k,vector<li>& l,vector<li> r){
  int n=k.size();
  vector<pair<int,li>> lft,rgt;
  li s=0;
  rep(i,n){
    if(r[i]<l[i]){
      lft.push_back({k[i],l[i]-r[i]});
      s+=r[i];
    }
    else if(l[i]<r[i]){
      rgt.push_back({n-k[i],r[i]-l[i]});
      s+=l[i];
    }else{
      s+=l[i];
    }
  }
  print(s+calc(rgt)+calc(lft));
}

int main(){
  int t; cin >>t;
  rep(_,t){
    int n; cin >>n;
    vector<int> k(n);
    vector<li> l(n),r(n);
    rep(i,n){
      cin >>k[i]>>l[i]>>r[i];
    }
    solve(k,l,r);
  }
}
