#pragma GCC target ("avx2")
#pragma GCC optimize ("unroll-loops")
#pragma GCC optimize ("O3")
#include "bits/stdc++.h"
#include <unordered_set>
#include <unordered_map>
#include <random>
using namespace std;
typedef long long ll;
const ll MOD = 1'000'000'007LL; /*998'244'353LL;*/
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for(int (i)=0; (i)<(n); (i)++)
const int dx[4]={ 1,0,-1,0 };
const int dy[4]={ 0,1,0,-1 };

int T;

signed main(){
    cin >> T;
    while(T--){
        int N;
        cin >> N;
        vector<pair<int, pair<ll, ll>>> vl, vr;
        rep(i, N){
            int K; ll L, R;
            cin >> K >> L >> R;
            if(L >= R) vl.pb({ K,{L,R} });
            else vr.pb({ N-K, {R, L} });
        }
        sort(all(vl));
        sort(all(vr));
        
        priority_queue<ll> pq;
        ll ans = 0;
        rep(i, vl.size()){
            if(i < vl[i].first){
                ans += vl[i].second.first;
                pq.push(vl[i].second.second-vl[i].second.first);
            }
            else{
                if(pq.size() > 0 && vl[i].second.second-vl[i].second.first < pq.top()){
                    ans += vl[i].second.first;
                    pq.push(vl[i].second.second-vl[i].second.first);
                    ans += pq.top();
                    pq.pop();
                }
                else{
                    ans += vl[i].second.second;
                }
            }
        }

        while(!pq.empty()) pq.pop();
        rep(i, vr.size()){
            if(i < vr[i].first){
                ans += vr[i].second.first;
                pq.push(vr[i].second.second-vr[i].second.first);
            }
            else{
                if(pq.size() > 0 && vr[i].second.second-vr[i].second.first < pq.top()){
                    ans += vr[i].second.first;
                    pq.push(vr[i].second.second-vr[i].second.first);
                    ans += pq.top();
                    pq.pop();
                }
                else{
                    ans += vr[i].second.second;
                }
            }
        }

        cout << ans << endl;
    }
}