#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#include <ext/pb_ds/assoc_container.hpp> 
#include <ext/pb_ds/tree_policy.hpp> 
using namespace __gnu_pbds;   
#define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update> 
#define ld long double
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define all(a) a.begin(),a.end()
#define sz(a) (ll)(a.size())
#define PII pair<ll,int>
const int M = 2e5+5;
vector<int> vec[M];
int k[M],val[M];
ll L[M],R[M];
bool dn[M];
int main()
{
	ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	int test;
	cin>>test;
	while(test--)
	{
		int n;
		cin>>n;
		for(int i=1;i<=n;i++)
		{
			vec[i].clear();
			val[i]=-1;
			dn[i]=0;
		}
		for(int i=1;i<=n;i++)
		{
			cin>>k[i]>>L[i]>>R[i];
			vec[k[i]].pb(i);
		}
		priority_queue<PII> pq;
		vector<int> v;
		for(int i=n;i>=1;i--)
		{
			for(auto u:vec[i])
			{
				if(L[u]>R[u])
					pq.push({L[u]-R[u],u});
			}
			if(!pq.empty())
			{
				auto P = pq.top();
				pq.pop();
				v.pb(P.ss);
				dn[P.ss] = 1;
			}
		}
		reverse(all(v));
		for(int c=0;c<sz(v);c++)
		{
			val[c+1] = v[c];
		}
		priority_queue<PII> rpq;
		while(!pq.empty())
		{
			auto P = pq.top();
			rpq.push({-P.ff,P.ss});
			pq.pop();
		}
		int c = sz(v)+1;
		for(int i=1;i<=n;i++)
		{
			for(auto u:vec[i])
			{
				if(!dn[u])
					rpq.push({R[u]-L[u],u});
			}
			if(!rpq.empty())
			{
				auto P = rpq.top();
				rpq.pop();
				val[c++] = P.ss;
			}
		}
		ll ans=0;
		for(int i=1;i<=n;i++)
		{
			int x = val[i];
			if(i>k[x])
				ans += R[x];
			else
				ans += L[x];
		}
		cout<<ans<<"\n";
	}
	return 0;
}