#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0; i<(n); i++)
using P = pair<int,int>;
using ll = long long;
P lp[200005], rp[200005];
int q, n, k, l, r, le, re;
int main(){
	scanf("%d", &q);
	rep(i,q){
		scanf("%d", &n);
		ll ans = le = re = 0;
		priority_queue<int> lqu, rqu;
		rep(i,n){
			scanf("%d%d%d", &k, &l, &r);
			(l < r ? rp[re++] : lp[le++]) = P{k,abs(l-r)};
			ans += min(l,r);
		}
		sort(lp, lp+le, greater<P>());
		sort(rp, rp+re);
		for(int i=le, le2=0; i>0; i--){
			while(le2 < le && lp[le2].first >= i) lqu.push(lp[le2++].second);
			if(!lqu.empty()){
				ans += lqu.top();
				lqu.pop();
			}
		}
		for(int i=le, re2=0; i<n; i++){
			while(re2 < re && rp[re2].first <= i) rqu.push(rp[re2++].second);
			if(!rqu.empty()){
				ans += rqu.top();
				rqu.pop();
			}
		}
		printf("%lld\n", ans);
	}
}