// clang-format off
	#include <bits/stdc++.h>
	using namespace std; using ll = long long; using uint = unsigned int;
	#define SFINAE(x, ...)	template <class, class = void> struct x : false_type {}; template <class T> struct x<T, void_t<__VA_ARGS__>> : true_type {};
	SFINAE(DefaultIO, decltype(cout << declval<T &>()))
	SFINAE(IsTuple, typename tuple_size<T>::type)
	SFINAE(Iterable, decltype(begin(declval<T>())))
	template <class T> constexpr char Space(const T &) { return (Iterable<T>::value or IsTuple<T>::value) ? '\n' : ' '; }
	template <auto &os> struct Writer {
		template <class T> void Impl(T const &t) const {
			if constexpr (DefaultIO<T>::value) os << t;
			else if constexpr (Iterable<T>::value) { int i = 0; for (auto &&x : t) ((i++) ? (os << Space(x), Impl(x)) : Impl(x)); }
			else if constexpr (IsTuple<T>::value) apply([this](auto const &... args) { int i = 0; (((i++) ? (os << ' ', Impl(args)) : Impl(args)), ...);}, t);
			else static_assert(IsTuple<T>::value, "No matching type for print");
		}
		template <class F, class... Ts> auto &operator()(F const &f, Ts const &... ts) const { return Impl(f), ((os << ' ', Impl(ts)), ...), os << '\n', *this; }
	};
	template <auto &is> struct Reader {
		template <class T> auto& operator()(T &t) const {
			if constexpr (DefaultIO<T>::value) is >> t;
			else if constexpr (Iterable<T>::value) for (auto &x : t) (*this)(x);
			else if constexpr (IsTuple<T>::value) apply([this](auto &... args) { ((*this)(args), ...); }, t);
			else static_assert(IsTuple<T>::value, "No matching type for read");
			return *this;
		}
		template <class T> auto operator[](T t) const { return (*this)(t), t; }
	};
	#ifdef NARUT_LOCAL
	#define Debug(...) cerr << "\033[0;31m", Writer<cerr>{}(__VA_ARGS__), cerr << "\033[0m\n"
	#else
	#define Debug(...) 0
	#endif
	[[maybe_unused]] constexpr Reader<cin> Cin; [[maybe_unused]] constexpr Writer<cout> Cout;
	#define FOR(i, j, n) for (int i = int(j); i < int(n); ++i)
	#define ROF(i, j, n) for (int i = int(n) - 1; i >= int(j); --i)
// clang-format on

struct Decrement {
	auto operator()(int x) const { return x - 1; }
};

template <class NextIndex = Decrement> struct PathCompressor : private NextIndex {
	vector<int> par;
	PathCompressor() = default;
	PathCompressor(int n, NextIndex next_index = {}) : NextIndex(next_index), par(n) { iota(begin(par), end(par), 0); }

	auto Find(int u) {
		while (par[u] != u) u = par[u] = par[par[u]];
		return u;
	}
	auto Use(int u) {
		auto v = Find(u);
		par[v] = (*this)(v);
		return v;
	}
};

auto Main() {
	const int n = Cin[0];
	using D = vector<array<int, 2>>;
	D good, bad;
	ll base = 0;
	FOR(ni, 0, n) {
		int k = Cin[0], l = Cin[0], r = Cin[0];
		if (l > r) {
			good.push_back({l - r, k});
			base += r;
		} else {
			bad.push_back({r - l, n - k});
			base += l;
		}
	}
	auto const Do = [](D &d) {
		sort(rbegin(d), rend(d));
		const int sz = d.size();
		PathCompressor<> pc(sz+1);
		ll ans = 0;
		for (auto [x, k] : d) {
			k = min(k, sz);
			auto u = pc.Find(k);
			if (u == 0) continue;
			ans += x;
			pc.Use(u);
		}
		return ans;
	};
	cout << base + Do(good) + Do(bad) << '\n';
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	cout << fixed << setprecision(10);
#ifdef NARUT_LOCAL
	assert(freopen("io/test.in", "r", stdin));
	cin.exceptions(cin.badbit | cin.failbit);
	cerr << fixed << setprecision(10);
#endif

	int n_cases;
	for (cin >> n_cases; n_cases > 0; n_cases--) {
		Main();
	}
}


Measured memory_bytes: 5112