#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <queue>
#include <stack>
#include <map>
#include <algorithm>
#include <math.h>
#include <cassert>
#define rep(i,n) for(int i = 0; i < n; ++i )
using namespace std;
using ll = long long;
using P = pair<int,ll>;

int main() {
  int t;
  cin >> t;
  rep(_,t){
    int n;
    cin >> n;
    ll ans = 0;
    vector<P> vl,vr;
    rep(i,n){
      ll k,l,r;
      cin >> k >> l >> r;
      ans += min(l,r);
      if(l>r) vl.emplace_back(k,l-r);
      if(l<r) vr.emplace_back(n-k,r-l);
    }
    sort(vl.rbegin(),vl.rend());
    sort(vr.rbegin(),vr.rend());
    int i=0; priority_queue<ll> q;
    rep(_k,int(vl.size())){
      int k = vl.size()-_k;
      while(i<vl.size()&&vl[i].first>=k) q.emplace(vl[i++].second);
      if(!q.empty()) ans+=q.top(),q.pop();
    }
    i=0; q = priority_queue<ll>();
    rep(_k,int(vr.size())){
      int k = vr.size()-_k;
      while(i<vr.size()&&vr[i].first>=k) q.emplace(vr[i++].second);
      if(!q.empty()) ans+=q.top(),q.pop();
    }
    cout << ans << endl;
  }
}
