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

void solve() {
    int n;
    cin >> n;
    ll ans = 0;
    vector<ll> lefters[n], righters[n+1];
    rep(i, n) {
        ll k, l, r;
        cin >> k >> l >> r;
        k--;
        ans += 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()) {
        ans += pql.top();
        pql.pop();
    }
    while (!pqr.empty()) {
        ans += pqr.top();
        pqr.pop();
    }
    cout << ans << endl;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int t;
    cin >> t;
    while (t--) {
        solve();
    }
}