#include <bits/stdc++.h>

using namespace std;

using ll = long long;

constexpr char newl = '\n';

ll calc(int n, vector< vector<int> >& vs) {
    priority_queue<int, vector<int>, greater<int> > pq;
    for (int i = 1; i <= n; i++) {
        for (int x : vs[i]) {
            pq.push(x);
        }

        while (pq.size() > i) pq.pop();
    }

    ll res = 0;
    while (!pq.empty()) {
        res += pq.top();
        pq.pop();
    }
    return res;
}

ll solve(int n, vector<int>& K, vector<int>& L, vector<int>& R) {
    ll ans = 0;
    vector< vector<int> > vs1(n + 1), vs2(n + 1);
    for (int i = 0; i < n; i++) {
        ans += min(L[i], R[i]);
        if (L[i] >= R[i]) vs1[K[i]].push_back(L[i] - R[i]);
        else vs2[n - K[i]].push_back(R[i] - L[i]);
    }

    ans += calc(n, vs1) + calc(n, vs2);
    return ans;
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    int t;
    cin >> t;

    for (int loop = 0; loop < t; loop++) {
        int n;
        cin >> n;

        vector<int> K(n), L(n), R(n);
        for (int i = 0; i < n; i++) {
            cin >> K[i] >> L[i] >> R[i];
        }

        cout << solve(n, K, L, R) << newl;
    }

    return 0;
}