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

struct tp
{
	ll l, r, k;
};

tp a[200010];
vector <tp> v1, v2;

bool cmp1(tp a, tp b)
{
	return a.k<b.k;
}
bool cmp2(tp a, tp b)
{
	return a.k>b.k;
}
int main()
{
	ll i, j, k, n, m, t;

	scanf("%lld", &t);

	while(t--)
	{
		scanf("%lld", &n);
		v1.clear();
		v2.clear();
		for(i=0; i<n; i++)
		{
			scanf("%lld %lld %lld", &a[i].k, &a[i].l, &a[i].r);
			//cout<<i<<endl;
			if(a[i].l>=a[i].r) v1.push_back(a[i]);
			else v2.push_back(a[i]);
		}

		sort(v1.begin(), v1.end(), cmp1);
		ll sz=v1.size(), cnt=1;
		priority_queue <ll> pq;
		ll res=0;
		//cout<<"*"<<endl;
		for(i=0; i<sz; i++)
		{
			res+=v1[i].l;
			if(v1[i].k>=cnt)
			{
				cnt++;
				pq.push(v1[i].r-v1[i].l);
			}
			else 
			{
				pq.push(v1[i].r-v1[i].l);
				ll x=pq.top();
				pq.pop();
				res+=x;
			}
		}

		while(pq.size()) pq.pop();

		sort(v2.begin(), v2.end(), cmp2);

		cnt=n;
		sz=v2.size();
		for(i=0; i<sz; i++)
		{
			res+=v2[i].r;
			if(v2[i].k<cnt)
			{
				cnt--;
				pq.push(v2[i].l-v2[i].r);
			}
			else
			{
				pq.push(v2[i].l-v2[i].r);
				ll x=pq.top();
				pq.pop();
				res+=x;
			}
		}
		while(pq.size()) pq.pop();

		printf("%lld\n", res);

	}



	return 0;
}