#include<bits/stdc++.h>
#define F first
#define S second
#define ll long long
#define MOD 1000000007
#define fast ios_base::sync_with_stdio(false)
#define itt cin.tie(0)
#define now cout.tie(0)
#define N 200010
using namespace std;
ll T,n,l[N],k[N],r[N],ans;
vector<pair<pair<ll,ll>,ll> > vv,bk;
int main(){
	fast;itt;now;
	ll i;
	cin>>T;
	while(T--)
	{
		cin>>n;
		ans=0;
		vv.clear();
		bk.clear();
		for(i=0;i<n;i++)
		{
			cin>>k[i]>>l[i]>>r[i];
			if(l[i]>=r[i])
			{
				vv.push_back(make_pair(make_pair(k[i],l[i]-r[i]),i));
			}
			else
			{
				bk.push_back(make_pair(make_pair(k[i],r[i]-l[i]),i));
			}
		}
		sort(vv.begin(),vv.end());
		sort(bk.begin(),bk.end());
		reverse(bk.begin(),bk.end());
		priority_queue<ll> pq;
		for(i=0;i<vv.size();i++)
		{
			ans+=l[vv[i].S];
			pq.push(-vv[i].F.S);
			if(pq.size()>vv[i].F.F)
			{
				ans+=pq.top();
				pq.pop();
			}
		}
		while(!pq.empty()) pq.pop();
		for(i=0;i<bk.size();i++)
		{
			ans+=r[bk[i].S];
			pq.push(-bk[i].F.S);
			if(n-pq.size()+1<=bk[i].F.F)
			{
				ans+=pq.top();
				pq.pop();
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}