#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;

//template
#define rep(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define ALL(v) (v).begin(),(v).end()
typedef long long int ll;
const int inf = 0x3fffffff; const ll INF = 0x1fffffffffffffff; const double eps=1e-12;
template<typename T>inline bool chmax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
template<typename T>inline bool chmin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
//end


void solve(){
   int n; cin>>n;
   vector<int> a(n),l(n),r(n);
   vector<vector<int>> lb(n),rb(n);
   ll res=0;
   rep(i,0,n){
      cin>>a[i]>>l[i]>>r[i]; a[i]--;
      res+=min(l[i],r[i]);
      if(l[i]<r[i]){if(a[i]+1<n)rb[a[i]+1].push_back(r[i]-l[i]);}
      else lb[a[i]].push_back(l[i]-r[i]);
   }
   
   priority_queue<int> pq;
   for(int i=n-1;i>=0;i--){
      for(int x:lb[i])pq.push(x);
      if(!pq.empty()){
         res+=pq.top(); pq.pop();
      }
   }
   while(pq.size())pq.pop();
   for(int i=0;i<n;i++){
      for(int x:rb[i])pq.push(x);
      if(!pq.empty()){
         res+=pq.top(); pq.pop();
      }
   }
   cout<<res<<endl;
}

int main(){
   int t; cin>>t;
   while(t--)solve();
   return 0;
}