#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < ll(n); i++)

vector<ll> ans;

void solve() {
    int n;
    cin >> n;
    ll res = 0;
    vector<ll> lefters[n], righters[n+1];
    rep(i, n) {
        ll k, l, r;
        cin >> k >> l >> r;
        k--;
        res += min(l, r);
        if (l > r) {
            lefters[k].push_back(l - r);
        } else {
            righters[k+1].push_back(r - l);
        }
    }
    priority_queue<ll, vector<ll>, greater<ll>> pql, pqr;
    rep(i, n) {
        for (ll val : lefters[i]) pql.push(val);
        while ((ll)pql.size() > i + 1) pql.pop();

        for (ll val : righters[n - i - 1]) pqr.push(val);
        while ((ll)pqr.size() > i + 1) pqr.pop();
    }
    while (!pql.empty()) {
        res += pql.top();
        pql.pop();
    }
    while (!pqr.empty()) {
        res += pqr.top();
        pqr.pop();
    }
    ans.push_back(res);
}

int main() {
    int t;
    cin >> t;
    while (t--) {
        solve();
    }
    for(ll a : ans){
        cout << a << endl;
    }
}