#include <cstdio>
#include <algorithm>
#include <vector>
#include <queue>

long long calc(std::vector<std::pair<int, int>> &in)
{
	long long r = 0;
	std::priority_queue<int> q;
	int index = in.size() - 1;
	for (int i = in.size(); i > 0; i -= 1)
	{
		while (index >= 0 && in[index].first >= i)
		{
			q.emplace(in[index].second);
			--index;
		}
		if (q.size() > 0)
		{
			r += (long long)q.top();
			q.pop();
		}
	}

	return r;
}

int main()
{
	int t;
	scanf("%d", &t);

	while (t-- > 0)
	{
		int n;
		scanf("%d", &n);
		std::vector<std::pair<int, int>> a;
		std::vector<std::pair<int, int>> b;
		long long ans = 0;
		for (int i = 0; i < n; ++i)
		{
			int k, l, r;
			scanf("%d%d%d", &k, &l, &r);
			if (l > r)
			{
				ans += (long long)r;
				a.emplace_back(k, l - r);
			}
			else if (l < r)
			{
				ans += (long long)l;
				if (n != k)
				{
					b.emplace_back(n - k, r - l);
				}
			}
			else
			{
				ans += (long long)r;
			}
		}
		std::sort(a.begin(), a.end());
		std::sort(b.begin(), b.end());
		ans += calc(a) + calc(b);
		printf("%lld\n", ans);
	}

	return 0;
}
