#include <bits/stdc++.h>

using namespace std;

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

        vector<vector<int64_t>> LL(n), RR(n + 1);
        int64_t ans = 0;
        for (int i = 0; i < n; i++){
            if (L[i] > R[i])
                LL[K[i]].push_back(L[i] - R[i]);
            else
                RR[K[i] + 1].push_back(R[i] - L[i]);
            ans += max(L[i], R[i]);
        }

        priority_queue<int64_t, vector<int64_t>, greater<int64_t>> Q;
        for (int i = 0; i < n; i++){
            for (const auto &ll: LL[i])
                Q.push(ll);
            while (Q.size() > i + 1){
                ans -= Q.top();
                Q.pop();
            }
        }

        while (!Q.empty())
            Q.pop();
        for (int i = n; i >= 0; i--){
            for (const auto &rr: RR[i])
                Q.push(rr);
            while (Q.size() > n - i){
                ans -= Q.top();
                Q.pop();
            }
        }

        cout << ans << endl;
    }();
    }
}