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

#define lli long long int
#define REP(i,s,n) for(int i=s;i<n;i++)
#define NUM 2520
#define INF (1LL<<50)
#define DEBUG 1
#define mp(a,b) make_pair(a,b)
#define SORT(V) sort(V.begin(),V.end())
#define PI (3.141592653589794)
#define TO_STRING(VariableName) # VariableName
#define LOG(x) if(DEBUG)cout<<TO_STRING(x)<<"="<<x<<" "<<endl;
#define LOG2(x,y) if(DEBUG)cout<<TO_STRING(x)<<"="<<x<<" "<<TO_STRING(y)<<"="<<y<<endl;
#define LOG3(x,y,z) if(DEBUG)cout<<TO_STRING(x)<<"="<<x<<" "<<TO_STRING(y)<<"="<<y<<" "<<TO_STRING(z)<<"="<<z<<endl;
#define LOG4(w,x,y,z) if(DEBUG)cout<<TO_STRING(w)<<"="<<w<<" "<<TO_STRING(x)<<"="<<x<<" "<<TO_STRING(y)<<"="<<y<<" "<<TO_STRING(z)<<"="<<z<<endl;

template<class T>bool chmax(T & a, const T & b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }

void solve(){

    lli ans = 0;
    priority_queue<pair<lli,lli>> q;
    priority_queue<pair<lli,lli>,vector<pair<lli,lli>>,greater<pair<lli,lli>>> qq;

    lli n;
    cin>>n;
    REP(i,0,n){
        lli k,l,r;
        cin>>k>>l>>r;
        if(l-r>0){
            q.push({k,l-r});
            ans += r;
        }
        else{
            qq.push({k,r-l});
            ans += l;
        }
    }

    lli now = 0;

    multiset<lli> s;
    lli cnt = 0;
    for(lli i=n;i>=1;i--){
        while(q.size() && q.top().first >= i){
            s.insert(q.top().second);
            q.pop();
        }
        if(s.size()==0)continue;
        ans += (*s.rbegin());
        s.erase(prev(s.end());
    }

    s.clear();
    for(lli i=cnt+1;i<=n;i++){
        while(qq.size() && qq.top().first < i){
            s.insert(qq.top().second);
            qq.pop();
        }
        if(s.size()==0)continue;
        ans += (*s.rbegin());
        s.erase(prev(s.end()));
    }
    cout<<ans<<endl;

}


int main(){

    lli t;
    cin>>t;
    while(t--)solve();

    return 0;
}
