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


const int N = 2e5 + 7;

vector <int> a[N], b[N];

void solve() {
	int n; cin >> n;
	for(int i = 1; i <= n; i++) {
		a[i].clear();
		b[i].clear();
	}
	long long ans = 0;
	for(int i = 1; i <= n; i++) {
		int k, l, r; cin >> k >> l >> r;
		ans += min(l, r); int x = min(l, r); l -= x; r -= x;
		if(l) {
			a[k].push_back(l);
		}
		if(r) {
			b[k + 1].push_back(r);
		}
	}
	priority_queue <int> pq;
	for(int i = 1; i <= n; i++) {
		while(b[i].size()) {
			pq.push(b[i].back()); b[i].pop_back();
		}
		if(pq.size()) {
			ans += pq.top(); pq.pop();
		}
	}
	while(pq.size()) {
		pq.pop();
	}
	for(int i = n; i >= 1; i--) {
		while(a[i].size()) {
			pq.push(a[i].back()); a[i].pop_back();
		}
		if(pq.size()) {
			ans += pq.top(); pq.pop();
		}
	}

	cout << ans << endl;
}

int main() {
	ios_base :: sync_with_stdio(0); cin.tie(0); cout.tie(0);
	int test, tc = 0; cin >> test;
	while(test--) {
		solve();
	}	
}