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

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	int t;
	cin >> t;
	while (t--) {
		int n;
		cin >> n;

		long long ans = 0;
		vector<pair<int, int>> right, left;
		for (int i = 0; i < n; ++i) {
			int k, l, r;
			cin >> k >> l >> r;
			ans += min(l, r);
			if (l < r) {
				right.emplace_back(r - l, n - k);
			} else if (l > r) {
				left.emplace_back(l - r, k);
			}
		}
		sort(right.rbegin(), right.rend());
		sort(left.rbegin(), left.rend());

		auto proc = [&ans, &n](vector<pair<int, int>> left) {
			set<int> ndx;
			for (int i = 1; i <= n; ++i) {
				ndx.insert(i);
			}
			for (auto [x, k] : left) {
				auto it = ndx.upper_bound(k);
				if (it != ndx.begin()) {
					ndx.erase(--it);
					ans += x;
				}
			}
		};

		proc(left);
		proc(right);
		cout << ans << "\n";
	}
}
