#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;
typedef pair<ll,ll> pll;
const ll mod=1e9+7;
//const ll mod=998244353;
const ll inf=1LL<<61;

int main() {
  ll t;
  cin >> t;
  while(t--) {
    ll n;
    cin >> n;
    priority_queue<ll,vec,greater<ll>> q;
    ll ans=0;
    mat a(n),b(n+1);
    for(ll i=0;i<n;i++) {
      ll k,l,r;
      cin >> k >> l >> r;
      k--;
      ans+=min(l,r);
      if(l-r>0) {
        a[k].push_back(l-r);
      }
      else if(l-r<0) {
        b[k+1].push_back(r-l);
      }
    }
    for(ll i=0;i<n;i++) {
      for(ll v:a[i]) {
        q.push(v);
      }
      while(q.size()>i+1) q.pop();
    }
    while(q.size()) {
      ans+=q.top();
      q.pop();
    }
    for(ll i=n-1;i;i--) {
      for(ll v:b[i]) {
        q.push(v);
      }
      while(q.size()>n-i) q.pop();
    }
    while(q.size()) {
      ans+=q.top();
      q.pop();
    }
    cout << ans << endl;
  }
}