#include<bits/stdc++.h>
using namespace std;
#define PI 3.141592653589793238462
typedef long long ll;
typedef double db;
vector<ll> p[200005],c[200005];
int main(){
    int o;cin>>o;
    while(o--){
        ll n,ans=0;cin>>n;
        priority_queue<ll,vector<ll>,greater<ll>> q;
        for(int i=1;i<=n;i++){
            p[i].clear();c[i].clear();
        }
        for(ll i=1;i<=n;i++){
            ll k,l,r;cin>>k>>l>>r;
            if(l>r) p[k].push_back(l-r),ans+=r;
            if(l<r) c[n-k].push_back(r-l),ans+=l;
            if(l==r) ans+=l;
        }
        for(int i=1;i<=n;i++){
            for(auto j:p[i]){
                q.push(j);
            }
            while(q.size()>i){
                q.pop();
            }
        }
        while(q.size()){
            ans+=q.top();q.pop();
        }
        for(int i=1;i<=n;i++){
            for(auto j:c[i]){
                q.push(j);
            }
            while(q.size()>i){
                q.pop();
            }
        }
        while(q.size()){
            ans+=q.top();q.pop();
        }
        cout<<ans<<endl;
    }
}