#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define ALL(v) (v).begin(), (v).end()
using ll = long long;
constexpr int INF = 1e9;
constexpr long long LINF = 1e18;
constexpr long long MOD = 1e9 + 7;

void solve() {
    int n;
    cin >> n;
    int k;
    ll l, r;
    ll ans = 0;
    vector<ll> vl[n + 2], vr[n + 2];
    rep(i, n) {
        cin >> k >> l >> r;
        if (l > r) {
            ans += r;
            vl[k].emplace_back(l - r);
        } else {
            ans += l;
            vr[k + 1].emplace_back(r - l);
        }
    }
    priority_queue<int, vector<int>, greater<int>> que;
    for (int i = 1; i <= n; i++) {
        for (ll x : vl[i]) {
            que.emplace(x);
        }
        while ((int)que.size() > i) {
            que.pop();
        }
    }
    while (!que.empty()) {
        ans += que.top();
        que.pop();
    }
    for (int i = n; i > 0; i--) {
        for (ll x : vr[i]) {
            que.emplace(x);
        }
        while ((int)que.size() > n - i + 1) {
            que.pop();
        }
    }
    while (!que.empty()) {
        ans += que.top();
        que.pop();
    }
    cout << ans << endl;
}

signed main() {
    int number_of_testcases;
    cin >> number_of_testcases;
    while (number_of_testcases--) {
        solve();
    }
    return 0;
}