#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using vi=vector<int>;
using P = pair<int,int>;
using Graph = vector<vector<int>>;

ll solve(ll N, vector<pair<ll, ll>> &A){
    sort(A.begin(), A.end());
    priority_queue<ll, vector<ll>, greater<ll>> pq;
    ll ret = 0;
    for(auto x : A){
        ll k = x.first;
        ll v = x.second;

        ret += v;
        pq.push(v);
        if(pq.size() > k){
            ret -= pq.top();
            pq.pop();
        }
    }
    return ret;
}

int main(){
    ll T;
    cin >> T;
    rep(t, T){
        ll N;
        cin >> N;
        ll tot = 0;
        vector<pair<ll, ll>> A;
        vector<pair<ll, ll>> B;
        rep(i, N){
            ll K, L, R;
            cin >> K >> L >> R;
            if(L > R){
                A.emplace_back(K, L-R);
                tot += R;
            }else{
                B.emplace_back(N-K, R-L);
                tot += L;
            }
        }
        cout << tot + solve(N, A) + solve(N, B) << endl;
    }
    return 0;
}
