#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;

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

        priority_queue<pair<u64, pair<s64, s64>>> lside;
        priority_queue<pair<u64, pair<s64, s64>>> rside;

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

            if(L > R) {
                lside.push(make_pair(N-K, make_pair(L - R, R)));
            } else {
                rside.push(make_pair(K, make_pair(R - L, L)));
            }
        }
    
        u64 ans = 0;
        u64 l_ptr = 1;
        while(!lside.empty()) {
            auto k = N - lside.top().first;
            auto lr = lside.top().second.first;
            auto r = lside.top().second.second;
            if(k < l_ptr) { // already pass through threshold
                ans += r;
            } else {
                ans += lr + r;
                l_ptr++;
            }
            lside.pop();
        }
        
        u64 r_ptr = N;
        while(!rside.empty()) {
            auto k = rside.top().first;
            auto rl = rside.top().second.first;
            auto l = rside.top().second.second;
            if(k >= r_ptr) { // already pass through threshold
                ans += l;
            } else {
                ans += rl + l;
                r_ptr--;
            }
            rside.pop();
        }
        cout << ans << endl;
        
    }

    return 0;
}