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

template<typename T>
void out(T x) { cout << x << endl; exit(0); }
#define watch(x) cout << (#x) << " is " << (x) << endl





using ll = long long;

const ll mod = 1e9+7;
const int maxn = 1e6 + 5;



ll solve() {
    int n;
    cin>>n;
    vector<int> l(n),r(n);
    vector<vector<int>> evL(n+10), evR(n+10);
    
    ll res = 0;
    for (int i=0; i<n; i++) {
	int k;
	cin>>k>>l[i]>>r[i];
	if (l[i]>=r[i]) {
	    evL[k].push_back(i);
	} else {
	    evR[k+1].push_back(i);
	}
	res += min(l[i],r[i]);
    }

    // left
    {
	priority_queue<ll,vector<ll>,greater<ll>> pq;
	for (int i=1; i<=n; i++) {
	    for (int j: evL[i]) {
		pq.push(l[j]-r[j]);
		res += (l[j]-r[j]);
	    }
	    while ((int)pq.size() > i) {
		res -= pq.top();
		pq.pop();
	    }
	}
    }


    // right
    {
	priority_queue<ll,vector<ll>,greater<ll>> pq;
	for (int i=n; i>=1; i--) {
	    for (int j: evR[i]) {
		pq.push(r[j]-l[j]);
		res += (r[j]-l[j]);
	    }
	    while ((int)pq.size() > n-i+1) {
		res -= pq.top();
		pq.pop();
	    }
	}
    }

    return res;
}

int main() {
    ios_base::sync_with_stdio(false); cin.tie(0);  cout.tie(0);

    int t; cin>>t;
    while (t--) {
	cout<<solve()<<"\n";
    }
    
    
    return 0;
}
