#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#ifdef tabr
#include "library/debug.cpp"
#else
#define debug(...)
#endif

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int tt;
    cin >> tt;
    while (tt--) {
        int n;
        cin >> n;
        ll mn = 0;
        vector<vector<pair<int, ll>>> x(2);
        for (int i = 0; i < n; i++) {
            ll k, l, r;
            cin >> k >> l >> r;
            mn += min(l, r);
            if (l >= r) {
                l -= r;
                x[0].emplace_back(k, l);
            } else {
                r -= l;
                if (n - k <= 0) {
                    continue;
                }
                x[1].emplace_back(n - k, r);
            }
        }
        for (int id = 0; id < 2; id++) {
            priority_queue<ll, vector<ll>, greater<ll>> pq;
            sort(x[id].begin(), x[id].end());
            x[id].emplace_back(2e9, 0);
            for (int i = 0; i < x[id].size() - 1; i++) {
                pq.emplace(x[id][i].second);
                while (pq.size() > x[id][i].first) {
                    pq.pop();
                }
            }
            while (!pq.empty()) {
                mn += pq.top();
                pq.pop();
            }
        }
        cout << mn << '\n';
    }
    return 0;
}