#include<bits/stdc++.h>
#define sz(x) ((int)x.size())
#define pb push_back
#define ii pair<int,int>
#define iii pair<ii,int>
#define ppb pop_back
#define st first
#define nd second
#define ll long long
#define N 200005
#define inf 2000000005
#define MOD 1000000007
#define LOG 40
#define EPS 0.000000001
using namespace std;

int n;
int k[N], l[N], r[N], nxt[N];

int find(int x) {

	if(x == -1 or x == n or nxt[x] == x)
		return x;
	return nxt[x] = find(nxt[x]);

}

void solve() {

	cin >> n;
	ll ans = 0;
	vector<int> ind;
	for(int i = 0; i < n; i++) {
		cin >> k[i] >> l[i] >> r[i];
		ans += r[i];
		ind.pb(i);
	}
	sort(ind.begin(), ind.end(), [&](int i, int j) {
		return l[i] - r[i] > l[j] - r[j];
	});
	for(int i = 0; i < n; i++)
		nxt[i] = i;
	int L_cnt = 0;
	for(int i: ind) {
		if(l[i] - r[i] <= 0)
			break ;
		int L = find(k[i] - 1);
		if(L != -1) {
			ans += l[i] - r[i];
			nxt[L] = L - 1;
			r[i] = -1;
			L_cnt++;
		}
	}
	for(int i = 0; i < L_cnt; i++)
		nxt[i] = L_cnt;
	for(int i = L_cnt; i < n; i++)
		nxt[i] = i;
	reverse(ind.begin(), ind.end());
	for(int i: ind) {
		if(r[i] == -1)
			continue ;
		int R = find(k[i]);
		if(R != n) {
			nxt[R] = R + 1;
		}
		else {
			ans += l[i] - r[i];
		}
	}	
	cout << ans << "\n";

}

int main() {

	ios_base::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);

	int t;
	cin >> t;
	while(t--)
		solve();

}