#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef tuple<ll,ll,ll> tlll;

int main() {
    int T; cin >> T;
    while (T--) {
        int N; cin >> N;
        ll ans = 0;
        vector<vector<int>> vl(N+1);
        vector<vector<int>> vr(N+1);
        for (int i = 0; i < N; ++i) {
            ll k, l, r; cin >> k >> l >> r;
            if (l > r) {
                ans += r;
                vl[k].push_back(l-r);
            } else if (l < r) {
                ans += l;
                vr[k].push_back(r-l);
            } else {
                ans += l;
            }
        }
        priority_queue<int, vector<int>, greater<int>> pl;
        for (int i = 1; i <= N; ++i) {
            for (int x : vl[i]) {
                pl.push(x);
            }
            while (pl.size() > i) pl.pop();
        }
        while (!pl.empty()) {
            ans += pl.top();
            pl.pop();
        }
        priority_queue<int, vector<int>, greater<int>> pr;
        for (int i = N; 1 <= i; --i) {
            for (int x : vr[i]) {
                pr.push(x);
            }
            while (pr.size() > N-i) pr.pop();
        }
        while (!pr.empty()) {
            ans += pr.top();
            pr.pop();
        }
        cout << ans << endl;
    }
}