#include <bits/stdc++.h>

using namespace std;
using ll = long long;

ll solveSingleSide(vector<pair<int, ll>> yeeters) {
	sort(begin(yeeters), end(yeeters), [] (auto a, auto b) {
		if (a.first != b.first) return a.first < b.first;
		else return a.second > b.second;
	});
	ll value = 0;
	int place = 0;
	for (auto p_v : yeeters) {
//		printf("try take %d %lld\n", p_v.first, p_v.second);
		if (place < p_v.first){
			value += p_v.second;
			++place;
		}
	}
	return value;
}

ll solve() {
	int n; scanf("%d", &n);
	vector<ll> cutoff(n), kaliwa(n), kanan(n);
	for (int i = 0; i < n; ++i) scanf("%lld %lld %lld", &cutoff[i], &kaliwa[i], &kanan[i]);
	vector<pair<int, ll>> leftBias, rightBias;
	ll ans = 0;
	for (int i = 0; i < n; ++i) {
		ans += min(kaliwa[i], kanan[i]);
		if (kaliwa[i] > kanan[i]) {
			leftBias.emplace_back(cutoff[i], kaliwa[i] - kanan[i]);
		} else {
			rightBias.emplace_back(n - cutoff[i], kanan[i] - kaliwa[i]);
		}
	}
	return ans + solveSingleSide(leftBias) + solveSingleSide(rightBias);
}

int main() {
	int t; scanf("%d", &t);
	while (t--) printf("%lld\n", solve());
	return 0;
}
