#include<bits/stdc++.h>
#define pii pair<int,int>
#define F first
#define S second
#define ll long long
#define mp make_pair
using namespace std;
const int N=2e5+5;
ll ans;int n,T;vector<pii>L,R;
int l[N],r[N],k[N];pii p[N];
priority_queue<int,vector<int>,greater<int> >q;
ll cal(vector<pii>v)
{
	while(!q.empty())q.pop();
	ll res=0;
	sort(v.begin(),v.end());
	for(int i=0;i<v.size();i++)
	{
		res+=v[i].S;
		q.push(v[i].S);
		while(q.size()>v[i].F)res-=q.top(),q.pop();
	}
	return res;
}
int main()
{
	cin>>T;
	while(T--)
	{
		while(!q.empty())q.pop();
		ans=0;L.clear();
		cin>>n;R.clear();
		for(int i=0;i<n;i++)
		{
			cin>>k[i]>>l[i]>>r[i];
			ans+=min(l[i],r[i]);
			if(l[i]>r[i])
				L.push_back(mp(k[i],l[i]-r[i]));
			if(r[i]>l[i])
				R.push_back(mp(n-k[i],r[i]-l[i]));
		}
		cout<<ans+cal(L)+cal(R)<<"\n";
	}
	return 0;
}