#include <algorithm>
#include <iostream>
#include <list>
#include <forward_list>
#include <vector>

using namespace std;

struct Info {
	int k;
	int l;
	int r;
};

int main() {
	int T;
	cin >> T;

	vector<long long> resultList(T);

	for(int t = 0; t < T; t++) {
		int N;
		cin >> N;

		vector<Info> list;
		vector<Info> list2;
		for(int i = 0; i < N; i++) {
			int k, l, r;
			cin >> k >> l >> r;
			if(l - r >= 0) {
				list.push_back(Info{k - 1, l, r});
			} else {
				list2.push_back(Info{k - 1, l, r});
			}
		}
		sort(list.begin(), list.end(), [](auto &a, auto &b) {
			if((a.l - a.r) == (b.l - b.r)) {
				return a.k > b.k;
			}
			return (a.l - a.r) > (b.l - b.r);
		});
		sort(list2.begin(), list2.end(), [](auto &a, auto &b) {
			if((a.r - a.l) == (b.r - b.l)) {
				return a.k < b.k;
			}
			return (a.r - a.l) > (b.r - b.l);
		});

		long long result = 0;
		vector<int> check(list.size());
		for(int i = 0; i < list.size(); i++) {
			check[i] = i;
		}
		for(int i = 0; i < list.size(); i++) {
			auto itr = upper_bound(check.begin(), check.end(), list[i].k);
			if(itr == check.begin()) {
				result += list[i].r;
				continue;
			}
			itr--;
			check.erase(itr);
			result += list[i].l;
		}
		vector<int> check2(list2.size());
		for(int i = 0; i < list2.size(); i++) {
			check2[i] = N - 1 - i;
		}
		for(int i = 0; i < list2.size(); i++) {
			auto itr = upper_bound(check2.begin(), check2.end(), list2[i].k);
			if(itr == check2.end()) {
				result += list2[i].l;
				continue;
			}
			check2.erase(itr);
			result += list2[i].r;
		}
		resultList[t] = result;
	}

	for(int t = 0; t < T; t++) {
		cout << resultList[t] << endl;
	}

	return 0;
}
