#include <bits/stdc++.h>
#include <boost/multiprecision/cpp_int.hpp>
#include <boost/range/adaptor/reversed.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
typedef long long int ll;

#define PI (acos(-1))
#define REP(i,n) for(ll (i)=0;(i)<(n);(i)++)

#define dump(x)  cerr << #x << " = " << (x) << endl;
#define dumparr(x) cerr << #x ; for(auto elem: x){cerr << " " << elem;} cerr << endl;
#define dumppairs(x) cerr << #x ; for(auto elem: x){cerr << "(" << elem.first << " " << elem.second << ") ";} cerr << endl;
#define dumpline cerr << "**********************" << endl;
#define dumpwline(x) cerr << "******** " << #x << "=" << (x) << " ********" << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;
#define ARRAY_LENGTH(array) (sizeof(array) / sizeof(array[0]))

struct node{
    ll k;
    ll l;
    ll r;
    bool operator<( const node& right ) const {
        return l-r < (right.l-right.r);
    }
};


int main2(void){
    dumpline
    ll n; cin >> n;
    vector <node> nodes;
    set <ll> empties;
    REP(i, n){
        ll k; ll l; ll r;
        cin >> k >> l >> r;
        k--;
        nodes.push_back({k, l, r});
        empties.insert(i);
    }
    empties.insert(-1);
    empties.insert(n);
    sort(nodes.begin(), nodes.end());
    ll ans = 0;
    for (auto no: nodes){
        dump(ans)

        ll k = no.k; ll l = no.l; ll r = no.r;
        ll idx;
        if (l-r >= 0){
            break;
        }
        dump(k)
        idx = *(++empties.lower_bound(k));
        dump(idx)
        if (idx == n){
            ans += l;
            continue;
        }
        empties.erase(idx);
        ans += r;
    }
    dumpline
    REP(i, n) {
        empties.insert(i);
    }
    reverse(nodes.begin(), nodes.end());
    for (auto no: nodes){
        ll k = no.k; ll l = no.l; ll r = no.r;
        ll idx;
        if (l-r < 0){
            break;
        }
        dump(k)
        idx = *(--empties.upper_bound(k));
        dump(idx)
        if (idx == -1){
            ans += r;
            continue;
        }
        empties.erase(idx);
        ans += l;
    }
    cout << ans << endl;
    return 0;
}



int main(void){
    ll t; cin >> t;
    REP(_, t){
        main2();
    }
    return 0;
}