#include <stdio.h>
#include <iostream>
#include <vector>
#include <queue>
#include <stack>
#include <algorithm>

using ll = long long int;
const int INF = (1<<30);
const ll INFLL = (1ll<<60);
const ll MOD = (ll)(1e9+7);

#define l_ength size

void mul_mod(ll& a, ll b){
	a *= b;
	a %= MOD;
}

void add_mod(ll& a, ll b){
	a = (a<MOD)?a:(a-MOD);
	b = (b<MOD)?b:(b-MOD);
	a += b;
	a = (a<MOD)?a:(a-MOD);
}

std::vector<ll> v[225816][2];
std::priority_queue<ll, std::vector<ll>, std::greater<ll> > pq;

int main(void){
	int n,i,j,k,m[2],t,c;
	ll l,r,ans=0ll;
	std::cin >> c;
	while(c--){
		ans = 0ll;
		m[0] = 0; m[1] = 0;
		std::cin >> n;
		for(i=0; i<n; ++i){
			std::cin >> k >> l >> r;
			v[k][l<r].push_back(abs(r-l));
			ans += std::min(l,r);
			++m[l<r];
		}
		for(k=0; k<2; ++k){
			for(i=1; i<=m[k]; ++i){
				t = (k?(n-i):i);
				for(j=0; j<v[t][k].l_ength(); ++j){
					pq.push(v[t][k][j]);
				}
				while(pq.size()>i){
					pq.pop();
				}
			}
			while(!pq.empty()){
				ans += pq.top();
				pq.pop();
			}
			for(i=m[k]+1; i<=n; ++i){
				t = (k?(n-i):i);
				for(j=0; j<v[t][k].l_ength(); ++j){
					ans += v[t][k][j];
				}
			}
		}
		for(i=0; i<=n; ++i){
			for(k=0; k<2; ++k){
				v[i][k].clear();
			}
		}
		std::cout << ans << std::endl;
	}
	return 0;
}
