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

#define LL long long int
#define FASTIO ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
const int N = 2e5 + 5;
const int oo = 1e9 + 5;
const LL ooll = 1e18 + 5;

void solve() {
	int n;
	cin >> n;
	vector<array<int, 2>> v, w;
	LL ans = 0;
	for (int i=0;i<n;++i) {
		int k, l, r;
		cin >> k >> l >> r;
		if (l >= r) {
			v.push_back({k, l-r});
			ans += r;
		} else {
			w.push_back({n-k, r-l});
			ans += l;
		}
	}
	int vsz = v.size(), wsz = w.size(), vit = 0, wit = 0;
	sort(v.begin(), v.end()); sort(w.begin(), w.end());

	multiset<int> st;
	for (int i=1;i<=n;++i) {
		while(vit<vsz && v[vit][0] == i) {
			st.insert(v[vit][1]);
			++vit;
		}
		while(st.size() > i) st.erase(st.begin());
	}
	for (auto it : st) {
		ans += it;
	}

	st.clear();
	while(wit < wsz && w[wit][0] < 1) wit++;
	for (int i=1;i<=n;++i) {
		while(wit < wsz && w[wit][0] == i) {
			st.insert(w[wit][1]);
			++wit;
		}
		while(st.size() > i) st.erase(st.begin());
	}
	for (auto it : st) ans += it;
	cout << ans << '\n';
}

int main() {
	FASTIO;
	int T;
	cin >> T;
	while(T--) {
		solve();
	}
	return 0;
}