#include <bits/stdc++.h>
using namespace std;
						
#define fast_cin() ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define endl '\n'
#define int long long

const int N = 2e5 + 5;

vector<int> g[2][N];

void testcase() {
	int n; cin >> n;
	int ans = 0;
	for (int i = 0; i < n; ++i) {
		int k, l, r; cin >> k >> l >> r;
		if (l >= r) {
			g[0][k].push_back(l - r);
		} else {
			g[1][n - k].push_back({r - l});
		}
		ans += min(l, r);
	}
	for (int u : {0, 1}) {
		multiset<int> now;
		for (int k = 1; k <= n; ++k) {
			for (int &profit : g[u][k]) {
				now.insert(profit);
				ans += profit;
			}
			while ((int)now.size() > k) {
				ans -= *now.begin();
				now.erase(now.find(*now.begin()));
			}
		}
	}
	cout << ans << endl;
	for (int i = 1; i <= n; ++i) {
		g[0][i].clear(), g[1][i].clear();
	}
}
				
int32_t main() {
	fast_cin();  
	int T; cin >> T;
	while (T--) {
		testcase();
	}
	return 0;
}