#include <bits/stdc++.h>
#define INF 1000000000
#define LINF 1000000000000000000
#define MOD 1000000007
#define mod 998244353
#define INF63 1061109567
#define INF127 9187201950435737471
#define UINF 18446744073709551615
#define F first
#define S second
#define ll long long
#define N 500010
using namespace std;
ll n,k[N],l[N],r[N],ans;
vector<pair<ll,ll> > lf,rg;
priority_queue<ll> pq;
int main(){
	ll T,tp,i;
	scanf("%lld",&T);
	while(T--)
	{
		scanf("%lld%",&n);
		lf.clear();
		rg.clear();
		ans=0;
		for(i=0;i<n;i++)
		{
			scanf("%lld%lld%lld",&k[i],&l[i],&r[i]);
			k[i]--;
			if(r[i]>=l[i])
			{
				rg.push_back(make_pair(k[i],l[i]-r[i]));
				ans+=r[i];
			}
			else
			{
				lf.push_back(make_pair(k[i],r[i]-l[i]));
				ans+=l[i];
			}
		}
		sort(lf.begin(),lf.end());
		sort(rg.begin(),rg.end());
		reverse(rg.begin(),rg.end());
		tp=0;
		while(!pq.empty())
		{
			pq.pop();
		}
		for(i=0;i<lf.size();i++)
		{
			pq.push(lf[i].S);
			if(tp>lf[i].F)
			{
				ans+=pq.top();
				pq.pop();
			}
			else
			{
				tp++;
			}
		}
		tp=n-1;
		while(!pq.empty())
		{
			pq.pop();
		}
		for(i=0;i<rg.size();i++)
		{
			pq.push(rg[i].S);
			if(tp<=rg[i].F)
			{
				ans+=pq.top();
				pq.pop();
			}
			else
			{
				tp--;
			}
		}
		printf("%lld\n",ans);
	}
	return 0;
}