#include <algorithm>
#include <iostream>
#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.k == b.k) {
				return (a.l - a.r) > (b.l - b.r);
			}
			return a.k < b.k;
		});
		sort(list2.begin(), list2.end(), [](auto &a, auto &b) {
			if(a.k == b.k) {
				return (a.r - a.l) > (b.r - b.l);
			}
			return a.k > b.k;
		});

		long long result = 0;
		int top = 0;
		for(int i = 0; i < list.size(); i++) {
			if(list[i].k >= top) {
				result += list[i].l;
				top++;
			} else {
				result += list[i].r;
			}
		}
		int bottom = N - 1;
		for(int i = 0; i < list2.size(); i++) {
			if(list2[i].k < bottom) {
				result += list2[i].r;
				bottom--;
			} else {
				result += list2[i].l;
			}
		}
		resultList[t] = result;
	}

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

	return 0;
}
