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

int main() {
  int T;
  cin>>T;
  for(int i=0;i<T;i++){
    int N;
    int64_t Z=0;
    cin>>N;
    vector<pair<int,int64_t>> l;
    vector<pair<int,int64_t>> r;
    for(int i=0;i<N;i++){
      int a;
      int64_t b,c;
      cin>>a>>b>>c;
      if(b>c){
        Z+=c;
        l.push_back(make_pair(a,b-c));
      }
      else if(c>b&&a!=N){
        Z+=b;
        r.push_back(make_pair(N-a,c-b));
      }
      else{
        Z+=b;
      }
    }
    sort(l.begin(), l.end());
    sort(r.begin(), r.end());
    priority_queue<int64_t, vector<int64_t>, greater<int64_t>> pq;
    for(auto x:l){
      int a;
      int64_t b;
      tie(a,b)=x;
      pq.push(b);
      if(a<pq.size()){
        pq.pop();
      }
    }
    while(!pq.empty()){
      Z+=pq.top();
      pq.pop();
    }
    for(auto x:r){
      int a;
      int64_t b;
      tie(a,b)=x;
      pq.push(b);
      if(a<pq.size()){
        pq.pop();
      }
    }
    while(!pq.empty()){
      Z+=pq.top();
      pq.pop();
    }
    cout<<Z<<endl;
  }
}