#include <bits/stdc++.h>
using namespace std;
#defin int long long
using vec_int = vector<int>;
using P = pair<int,int>;
using Tu = tuple<int,int,int>;
using ll = long long;
#define rep(i, n) for(int i = 0; i < (int)(n); i++)

int charToInt(char c){
    char zero_num = '0';
    return (int)c - (int)zero_num;
}

signed main(){
    int T; cin>>T;
    for(int problem=0;problem<T;problem++){
        int N; cin>>N;
        vec_int K(N);
        vec_int L(N);
        vec_int R(N);
        vec_int diff(N); //このdiffはK[i]番目以内に入れたときの報酬

        int N1=0;
        int N2=0;
        for(int i=0;i<N;i++){
            cin>>K.at(i)>>L.at(i)>>R.at(i);
            diff.at(i) = L.at(i)-R.at(i); 
            if (diff.at(i)>=0){
                N1++;
            }else{
                N2++;
            }
        }
        vector<Tu> vec_T1(N1);// 前に並べる奴ら
        vector<Tu> vec_T2(N2);// 前に並べる奴ら
        int index1 = 0;
        int index2 = 0;
        for(int i=0;i<N;i++){
            if(diff.at(i)>=0){
                vec_T1.at(index1) = make_tuple(K.at(i),diff.at(i),L.at(i));
                index1++;
            }else{
                vec_T2.at(index2) = make_tuple(N-K.at(i),-1*diff.at(i),R.at(i));
                index2++;
            }
        }
        sort(vec_T1.begin(),vec_T1.end());
        sort(vec_T2.begin(),vec_T2.end());


        priority_queue<P> pq1;// 前に並べる奴ら
        priority_queue<P> pq2;// 後ろに並べる奴ら
        int tot_score = 0;

        for(int i=0;i<N1;i++){
            Tu temp = vec_T1.at(i);
            int K, diff, score;
            tie(K,diff,score) = temp;
            if(pq1.size()<K){
                pq1.push(make_pair(diff,score));
            }else{
                int min, sc;
                tie(min,sc) = pq1.top();
                if(min<diff){
                    pq1.pop();
                    pq1.push(make_pair(diff,score));
                    tot_score+=sc-min;
                }else{
                    tot_score+=score-diff;
                }
            }
        }
        for(int i=0;i<N2;i++){
            Tu temp = vec_T2.at(i);
            int K, diff, score;
            tie(K,diff,score) = temp;
            if(K==0){
                tot_score+=score-diff;
                continue;
            }
            if(pq2.size()<K){
                pq2.push(make_pair(diff,score));
            }else{
                int min, sc;
                tie(min,sc) = pq2.top();
                if(min<diff){
                    pq2.pop();
                    pq2.push(make_pair(diff,score));
                    tot_score+=sc-min;
                }else{
                    tot_score+=score-diff;
                }
            }
        }



        while(pq1.size()>0){
                int diff, sc;
                tie(diff,sc) = pq1.top(); pq1.pop();
                tot_score+=sc;
        }
        while(pq2.size()>0){
                int diff, sc;
                tie(diff,sc) = pq2.top(); pq2.pop();
                tot_score+=sc;
        }
        cout<<tot_score<<endl;
    }
    


    return 0;
}