#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define fast ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

struct node {
	ll l,r,k;
};

bool cmp(node &p, node &q) {
	return p.k < q.k;
}

int main() {
	ll t;
	cin>>t;
	while(t--) {
		ll n;
		cin>>n;
		ll i,ans=0,ind1=0,ind2=0;
		node p[n+1],q[n+1];
		for(i=1;i<=n;i++) {
			ll l,r,k;
			cin>>k>>l>>r;
			ans += min(l,r);
			if(l>r) {
				p[ind1].l=l;
				p[ind1].r=r;
				p[ind1].k=k;
				ind1++;
			}
			else {
				q[ind2].l=l;
				q[ind2].r=r;
				q[ind2].k=n-k;
				ind2++;
			}
		}
		// cout<<ind1<<" "<<ind2<<"\n";
		sort(p, p+ind1, cmp);
		sort(q, q+ind2, cmp);

		priority_queue<ll, vector<ll>, greater<ll>> pq1, pq2;
		for(i=0;i<ind1;i++) {
			if(p[i].k == 0)
				continue;
			else if(p[i].k > pq1.size())
				pq1.push(p[i].l - p[i].r);
			else if(p[i].k == pq1.size() && pq1.top()<(p[i].l-p[i].r)) {
				pq1.pop();
				pq1.push(p[i].l-p[i].r);
			}
		}
		for(i=0;i<ind2;i++) {
			if(q[i].k == 0)
				continue;
			else if(q[i].k > pq2.size())
				pq2.push(q[i].r - q[i].l);
			else if(q[i].k == pq2.size() && pq2.top()<(q[i].r-q[i].l)) {
				pq2.pop();
				pq2.push(q[i].r-q[i].l);
			}
		}
		while(!pq1.empty()) {
			ans += pq1.top();
			pq1.pop();
		}
		while(!pq2.empty()) {
			ans += pq2.top();
			pq2.pop();
		}
		cout<<ans<<"\n";
	}
} 