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

#define rep(i,x,y) for(ll i=(x);i<(y);i++)
#define rrep(i,x,y) for(ll i=(ll)(y)-1;i>=(x);i--)
#define all(x) (x).begin(),(x).end()
#define itrout(x) for(int i=0;i<x.size();i++) {cout << x[i] << (i==x.size()-1 ? "\n" : " ");}
#ifdef LOCAL
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl
#define itrdebug(x) cerr << #x << " "; for (auto & el : (x)) {cerr << (el) << " ";} cerr << endl
#define dassert(...) assert(__VA_ARGS__)
#else
#define debug(x)
#define itrdebug(x)
#define dassert(...)
#endif
//#define int long long

typedef long long ll;
const ll MOD = 1e9 + 7;
const long double EPS = 1e-8;

class BIT
{
public:
    vector<int> bit;
    int M;

    BIT(int M):
            bit(vector<int>(M+1, 0)), M(M) {}

    int sum(int i) {
        if (!i) return 0;
        return bit[i] + sum(i-(i&-i));
    }

    void add(int i, int x) {
        if (i > M) return;
        bit[i] += x;
        add(i+(i&-i), x);
    }
};


signed main(){
    int T;
    cin >> T;
    rep(ti,0,T) {
        int N;
        cin >> N;
        vector<int> K(N), L(N), R(N);
        rep(i,0,N) cin >> K[i] >> L[i] >> R[i];
        vector<pair<int, int>> LR(N);
        rep(i,0,N) LR[i] = {abs(L[i] - R[i]), i};
        sort(all(LR), greater<>());
        BIT bitL(N), bitR(N);
        ll answer = 0;
        rep(i,0,N) {
            int j = LR[i].second;
            if (L[j] - R[j] >= 0) {
                if (bitL.sum(K[j]) >= K[j]) {
                    answer += R[j];
                } else {
                    answer += L[j];
                    bitL.add(K[j], 1);
                }
            } else {
                if (bitR.sum(N-K[j]+1) >= N-K[j]) {
                    answer += L[j];
                } else {
                    answer += R[j];
                    bitR.add(N-K[j]+1, 1);
                }
            }
        }
        cout << answer << endl;
    }
    return 0;
}
