#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define pb push_back
#define mod(n,k) ( ( ((n) % (k)) + (k) ) % (k))
#define forn(i,a,b) for(int i = a; i < b; i++)
#define forr(i,a,b) for(int i = a; i >= b; i--)
#define all(x) (x).begin(), (x).end()

typedef long long ll;
typedef long double ld;
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;

struct info{
	int K,L,R;
	info(int _K,int _L,int _R){
		K = _K;
		L = _L;
		R = _R;
	}
	bool operator<(info &b)const{
		if(max(L,R) == max(b.L,b.R))return min(L,R) > min(b.L,b.R);
		return max(L,R) > max(b.L,b.R);
	}
};
int main(){
	ios_base::sync_with_stdio(0); cin.tie(0);
	int T; cin >> T;
	while(T--){
		int N; cin >> N;
		vector<info> A;
		forn(i,0,N){
			int K,L,R; cin >> K >> L >> R; K--;
			A.pb(info(K,L,R));
		}
		set<int> vivos;
		forn(i,0,N)vivos.insert(i);
		sort(all(A));
		ll res = 0;
		forn(i,0,N){
			if(A[i].L >= A[i].R){
				auto it = vivos.lower_bound(A[i].K);
				if(it == vivos.begin() && *it > A[i].K){
					vivos.erase(*it);
					res += A[i].R;
				}else{
					it--;
					vivos.erase(*it);
					res += A[i].L;
				}
			}else{
				auto it = vivos.lower_bound(A[i].K+1);
				if(it == vivos.end()){
					it--;
					vivos.erase(*it);
					res += A[i].L;
				}else{
					vivos.erase(*it);
					res += A[i].R;
				}
			}
		}
		cout << res << '\n';
	}
	return 0;
}
/*
__builtin_mul_overflow(x,y,&x)
-fsplit-stack
*/
