#include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
#define k first
#define v second
using namespace std;

const int N = 200005;
int n, T;

long long score(vector<pair<int, int>> &x)
{	if(x.empty()) return 0;
	long long res = 0;
	sort(x.begin(), x.end());
	priority_queue<int> q;
	int idx = x.size() - 1;
	for(int i = x[idx].k; i; --i)
	{
		while(idx >= 0 && x[idx].k == i) q.push(x[idx--].v);
		if(!q.empty())
		{
			res += q.top();
			q.pop();
		}
	}
	return res;
}

int main(){
	cin >> T;
	while(T--)
	{
		cin >> n;
		long long ans = 0;
		vector<pair<int, int>> L, R;
		for(int i = 0, k, l, r; i < n; ++i)
		{
			scanf("%d %d %d", &k, &l, &r);
			if(l <= r) {
				ans += l;
				R.push_back({n-k, r-l});
			}
			else
			{
				ans += r;
				L.push_back({k, l-r});
			}
		}
		ans += score(L);
		ans += score(R);
		printf("%lld\n", ans);
	}
	return 0;
}
