#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep_range(i, s, e) for (int i = (s); i < (int)(e); i++)
#define rep_rev(i, s, e) for (int i = (s)-1; i >= (int)(e); i--)
#define sort_vec(v) sort((v).begin(),(v).end())
typedef int64_t lint;
// lint mod = 998244353;
// lint mod = 1000000007;
template< typename T1, typename T2 >
inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }
template< typename T1, typename T2 >
inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }

void app() {
  int t;
  cin>>t;

  rep(_t,t){
    int n;
    cin>>n;

    lint ans=0;
    vector<multiset<lint>> cp(n+1),cm(n+1);
    rep(i,n){
      int k;
      lint l,r;
      cin>>k>>l>>r;
      if(l>r) cp[k].emplace(l-r),ans+=r;
      else cm[k].emplace(r-l),ans+=l;
    }

    priority_queue<lint,vector<lint>,greater<lint>> sp,sm;
    rep(k,n+1){
      for(lint c:cp[k]){
        ans+=c; sp.push(c);
        if(sp.size()>k){
          ans-=sp.top(); sp.pop();
        }
      }
    }

    rep_rev(k,n+1,0){
      for(lint c:cm[k]){
        ans+=c; sm.push(c);
        if(sm.size()>n-k){
          ans-=sm.top(); sm.pop();
        }
      }
    }

    cout<<ans<<endl;
  }

}

int main(){app();return 0;}
