#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using p = pair<ll, ll>;

// 
ll f(vector<p>& pp){
    // 独立に行っているのに、番号n-1(0-origin)より上は意味がないので矯正
    for(auto& elm : pp){
        elm.first = min(elm.first, (ll)(pp.size()));
    }

    vector<vector<ll> > s(pp.size()+1);
    for(auto& elm : pp){
        s[elm.first].push_back(elm.second);
    }
    ll res = 0;
    priority_queue<ll> q;
    for(int i = pp.size(); i >= 1; i--){
        for(ll x : s[i]) q.push(x);
        if(!q.empty()){
            res += q.top();
            q.pop();
        }
    }
    return res;
}

void solve(){
    int N;
    cin >> N;
    cin.ignore();
    ll ans = 0;
    vector<p> lp, rp;
    for(int i = 0; i < N; i++){
        ll k, l, r;
        cin >> k >> l >> r;
        cin.ignore();
        ll m = min(l, r);
        ans += m;
        l -= m; r -= m;
        if(l > 0){
            lp.emplace_back(k, l);
        } else {
            rp.emplace_back(N - k, r);
        }
    }
    ans += f(lp);
    ans += f(rp);
    cout << ans << endl;
    return;
}

int main(){

    int T;
    cin >> T;
    cin.ignore();

    for(int i = 0; i < T; i++){
        solve();
    }

    return 0;
}