#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

#define watch(x) cout<<(#x)<<"="<<(x)<<'\n'
#define mset(d,val) memset(d,val,sizeof(d))
#define setp(x) cout<<fixed<<setprecision(x)
#define forn(i,a,b) for(int i=(a);i<(b);i++)
#define fore(i,a,b) for(int i=(a);i<=(b);i++)
#define pb push_back
#define F first
#define S second
#define pqueue priority_queue
#define fbo find_by_order
#define ook order_of_key
typedef long long ll;
typedef pair<ll,ll> ii;
typedef pair<ll,ii> iii;
typedef vector<ll> vi;
typedef vector<ii> vii;
typedef long double ld;
typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> pbds;
void amin(ll &a, ll b){ a=min(a,b); }
void amax(ll &a, ll b){ a=max(a,b); }
void YES(){cout<<"YES\n";} void NO(){cout<<"NO\n";}
void SD(int t=0){ cout<<"PASSED "<<t<<endl; }
const ll INF = ll(1e18);
const int MOD = 998244353;

bool DEBUG = 0;
const int MAXN = 200005;

void solve()
{
	int n; cin>>n;
	vector<ll> v[n];
	ll ans=0;
	
	forn(i,0,n){
		int k; ll l,r; cin>>k>>l>>r; k--;
		if(l>=r){
			ans+=min(l,r);
			v[k].pb(abs(l-r));
		}
		else{
			ans+=max(l,r);
			v[k].pb(-abs(l-r));
		}
	}
	
	multiset<ll> q;
	multiset<ll> bad;
	forn(i,0,n)
	{
		sort(v[i].begin(),v[i].end());
		vector<ll> add;
		
		//add till i+1
		while(v[i].size() && v[i].back()>0 && q.size()+add.size()<=i){
			add.pb(v[i].back());
			v[i].pop_back();
		}
		
		while(q.size() && v[i].size() && *q.begin()<v[i].back()){
			bad.insert(*q.begin());
			q.erase(q.begin());
			add.pb(v[i].back());
			v[i].pop_back();
		}
		
		while(add.size()){ q.insert(add.back()); add.pop_back(); }
		
		while(v[i].size()){ bad.insert(v[i].back()); v[i].pop_back(); }
	}
	
	while(q.size()<n){
		q.insert(*bad.rbegin());
		bad.erase(--bad.end());
	}
	
	//watch(q.size());
	//assert(q.size()==n);
	//cout<<"q=";
	for(ll x: q){
		//cout<<x<<",";
		ans+=x;
	}
	//cout<<'\n';
	
	cout<<ans<<'\n';
}

int main()
{
	ios_base::sync_with_stdio(0); cin.tie(0);
	
	int t; cin>>t;
	forn(i,0,t) solve();
	
	return 0;
}
