#include<bits/stdc++.h>
#include<boost/dynamic_bitset.hpp>
using namespace std;
using u64 = uint64_t;
using s64 = int64_t;

int main(void) {
    u64 T;
    cin >> T;

    vector<u64> ans(T);

    for(u64 t=0; t<T; t++) {
        u64 N;
        cin >> N;

        priority_queue<pair<u64, u64>> lside;
        priority_queue<pair<u64, u64>> rside;
        u64 t_ans = 0;

        for(uint n=0; n<N; n++) {
            u64 K;
            u64 L, R;
            cin >> K >> L >> R;
            t_ans += min(L, R);

            if(L > R) {
                lside.push(make_pair(K, L-R));
            } else {
                // HACK : sort order defalut is N to M (N > M)
                rside.push(make_pair(N-K, R-L));
            }
        }
    
        priority_queue<u64> lcdt;
        for(u64 l=min(lside.size(), lside.top().first); l>0; l--) {
            while(!lside.empty() && l <= lside.top().first) {
                lcdt.push(lside.top().second);
                lside.pop();
            }
            if(!lcdt.empty()) {
                t_ans += lcdt.top();
                lcdt.pop();
            }   
        }
        
        priority_queue<u64> rcdt;
        for(u64 r=min(rside.size(), rside.top().first); r>0; r--) {
            while(!rside.empty() && r <= rside.top().first) {
                rcdt.push(rside.top().second);
                rside.pop();
            }
            if(!rcdt.empty()) {
                t_ans += rcdt.top();
                rcdt.pop();
            }   
        }

        ans[t] = t_ans;
    }


    for(const auto a : ans) cout << a << endl;
    return 0;
}