#pragma GCC optimize("Ofast",3,"inline")
#include<bits/stdc++.h>
using namespace std;
#define fir first
#define sec second
#define m_p make_pair
#define y1 ygftgfgcdtfgxffgx
#define y2 yfdsesgvtyghftfvv
#define x1 xvyr6cf6fgcfgf676
#define x2 xcr6rfc5r66y6r6fr
#define up_bound upper_bound
#define low_bound lower_bound
#define next_per next_permutation
#define pb push_back
#define i_to_s to_string
typedef priority_queue<int> p_queue;
typedef priority_queue<int, vector<int>, greater<int> > min_p_queue;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
int mon[20]={31,28,31,30,31,30,31,31,30,31,30,31};
ll gcd(ll x,ll y){return ((y==0)?x:gcd(y,x%y));}
inline int read(){
	int x=0;char ch=getchar();
	while(ch<'0'||ch>'9')ch=getchar();
	while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
	return x;
}
vector<pii >a,b;
ll ans;
int t,n,k,l,r;
multiset<int>s;
inline void f(vector<pii >&v){
	sort(v.begin(),v.end());
	s.clear();
	for(int i=0;i<s.size();i++){
		int x=v[i];
		s.insert(x.sec);
		ans+=x.sec;
		while(s.size()>x.first){
			ans-=*s.begin();
			s.erase(s.begin());
		}
	}
}
int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);
//	freopen(".in","r",stdin);
//	freopen(".out","w",stdout);
	cin>>t;
	while(t--){
		cin>>n;
		ans=0;
		a.clear(),b.clear();
		for(int i=1;i<=n;i++){
			cin>>k>>l>>r;
			if(l>r)a.pb(k,l-r);
			if(l<r)b.pb(n-k,r-l);
			ans+=min(l,r);
		}
		f(a),f(b);
		cout<<ans<<'\n';
	}
	return 0;
}