#include <bits/stdc++.h>

int ri() {
	int n;
	scanf("%d", &n);
	return n;
}

int64_t solve() {
	int n = ri();
	int64_t base = 0;
	std::vector<int> first[n];
	std::vector<int> second[n];
	int first_cnt = 0;
	int second_cnt = 0;
	for (int i = 0; i < n; i++) {
		int k = ri();
		int l = ri();
		int r = ri();
		if (k == n || l == r) base += l;
		else if (l > r) {
			base += r;
			first[k].push_back(l - r);
			first_cnt++;
		} else {
			base += l;
			second[k].push_back(r - l);
			second_cnt++;
		}
	}
	int64_t r0 = 0;
	std::multiset<int> all;
	for (int i = n - 1; i >= 0; i--) {
		if (all.size() && i < first_cnt) r0 += *std::prev(all.end()), all.erase(std::prev(all.end()));
		for (auto j : first[i]) all.insert(j);
	}
	all.clear();
	int64_t r1 = 0;
	for (int i = 0; i < n; i++) {
		for (auto j : second[i]) all.insert(j);
		if (all.size() && i >= n - second_cnt) r1 += *std::prev(all.end()), all.erase(std::prev(all.end()));
	}
	return base + r0 + r1;
}

int main() {
	int t = ri();
	for (int i = 0; i < t; i++) printf("%" PRId64 "\n", solve());
	return 0;
}
