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

using lint = long long;
using pint = pair<int, int>;
#define x first
#define y second

int N;

void exe(int tc) {
	lint ans = 0;

	vector<pint> a, b;

	cin >> N;
	for (int i=0; i<N; i++) {
		int k, l, r;
		cin >> k >> l >> r;
		if (l > r) {
			ans += r;
			a.emplace_back(k, l-r);
		} else {
			ans += l;
			b.emplace_back(N-k, r-l);
		}
	}

	sort(a.begin(), a.end());
	sort(b.begin(), b.end());

	priority_queue<int, vector<int>, greater<int> > qa, qb;

	for (pint p : a) {
		qa.push(p.y);
		ans += p.y;

		while (qa.size() > p.x) {
			ans -= qa.top();
			qa.pop();
		}
	}

	for (pint p : b) {
		qb.push(p.y);
		ans += p.y;

		while (qb.size() > p.x) {
			ans -= qb.top();
			qb.pop();
		}
	}

	cout << ans << '\n';
}

int main() {
	ios::sync_with_stdio(0); cin.tie(0);

	int tc;
	cin >> tc;
	for (int i=1; i<=tc; i++) exe(i);
}