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

int main() {
    int t;
    cin >> t;
    for(int i=0;i<t;i++){
        ll n,ans=0;
        cin >> n;
        vector<ll>k(n),l(n),r(n);
        vector<pair<int,int>> a,b;
        for(int i=0;i<n;i++){
            cin >> k[i] >> l[i] >> r[i];
            ans+=min(l[i],r[i]);
            if(l[i]>r[i]){
                a.push_back(make_pair(k[i],(l[i]-r[i])));
            }
            else{
                b.push_back(make_pair((n-k[i]),(r[i]-l[i])));
            }
        }
        sort(a.begin(),a.end());
        sort(b.begin(),b.end());
        priority_queue<ll,vector<ll>,greater<ll>>lpq,rpq;
        for(int i=0;i<a.size();i++){
            lpq.push(a[i].second);
            while(a[i].first<lpq.size()){
                lpq.pop();
            }
        }
        while(lpq.size()){
            ans+=lpq.top();
            lpq.pop();
        }
        for(int i=0;i<b.size();i++){
            rpq.push(b[i].second);
            while(b[i].first<rpq.size()){
                rpq.pop();
            }
        }
        while(rpq.size()){
            ans+=rpq.top();
            rpq.pop();
        }
        cout << ans << endl;
    }
}