#include<bits/stdc++.h>
using namespace std;
typedef long long  ll;
ll f(vector<pair<int,int> >a){
    ll ans=0;
    sort(a.begin(),a.end());
    priority_queue<int,vector<int>,greater<>>q;
    for(auto i:a){
        q.emplace(i.second);
        ans+=i.second;
        while(q.size()>i.first) ans-=q.top(),q.pop();
    }
    return ans;
}

int main(){
    int t,n,k,l,r;
    cin>>t;
    while(t--){
        cin>>n;
        ll sum=0;
        vector<pair<int,int> >L,R;
        for(int i=0;i<n;i++){
            cin>>k>>l>>r;
            if(l>r) L.emplace_back(k,l-r),sum+=r;
            else if(r>l) L.emplace_back(n-k,r-l),sum+=l;
            else sum+=l;
        }
        cout<<sum+f(L)+f(R)<<endl;
    }
	return 0;
}
