#include <iostream> 
using namespace std; 

#include <ext/pb_ds/assoc_container.hpp> 
#include <ext/pb_ds/tree_policy.hpp> 
using namespace __gnu_pbds; 
  
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define ll long long int 

bool comp(pair<ll,pair<int,int> >a,pair<ll,pair<int,int> > b )
{
	return a.first>b.first;
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
	int t;
	cin>>t;
	while(t--)
	{
		ll n,l,r,k;
		cin>>n;
		vector<pair<ll,pair<int,int> > > a;
		ll ans=0;
		for(int i=0;i<n;i++)
		{
				cin>>k>>l>>r;
				--k;
				if(l>r)
				{
					ans+=l;
					a.push_back({(l-r),{k,0}});
				}
				else if(r>l)
				{
					ans+=r;
					a.push_back({(r-l),{k,1}});
				}
				else
				ans+=l;
		}
		sort(a.begin(),a.end(),comp);
		ordered_set s1,s2;
		int cnt1=0;
		for(int i=0;i<a.size();i++)
		{
			if(a[i].second.second==1)
			{
				int l=s1.order_of_key(a[i].second.first+1);
				if(cnt1-l>(n-1-a[i].second.first-1))
				{
					ans-=(a[i].first);
				}
				else
				{
					++cnt1;
					s1.insert(a[i].second.first);
				}
			}
			else
			{
				int l=s2.order_of_key(a[i].second.first+1);
				if(l>(a[i].second.first))
				{
					ans-=(a[i].first);
				}
				else
				{
					s2.insert(a[i].second.first);
				}
			}
		}
		cout<<ans<<"\n"; 
	}
	return 0;
}