#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define lvector vector<ll>
#define cvector vector<char>
#define svector vector<string>
#define lque queue<ll>
#define lpque priority_queue<ll>
#define dlpque priority_queue<ll,lvector,greater<ll>>
#define P pair<ll,ll>
#define ALL(a) a.begin(),a.end()
#define RALL(a) a.rbegin(),a.rend()
#define rep(i,n) for(ll i=0; i<n; ++i)
#define print(a) cout << (a) << endl

ll solve(vector<P> v) {
  ll n=v.size(),ret=0;
  rep(i,n) v[i].first=min(v[i].first,n);
  vector<lvector> w(n+1);
  rep(i,n) w[v[i].first].emplace_back(v[i].second);
  lpque q;
  for(ll i=n;i>0;--i) {
    for(ll j:w[i]) q.push(j);
    if(q.size()) {ret+=q.top();q.pop();}
  }
  return ret;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  ll T,n,k,l,r,m,ans;cin>>T;
  rep(t,T) {
    cin>>n;ans=0;
    vector<P> A,B;
    rep(i,n) {
      cin>>k>>l>>r;
      m=min(l,r);ans+=m;
      l-=m,r-=m;
      if(l>0) A.emplace_back(k,l);else B.emplace_back(n-k,r);
    }
    ans+=solve(A);ans+=solve(B);
    print(ans);
  }
  return 0;
}