#include<iostream>
#include<cstdio>
#include<queue>
#include<algorithm>
#define N 200005
#define LL long long
using namespace std;

struct node
{
	int k,len;
}p[N];
priority_queue<node>q;
bool operator<(node x,node y)
{
	return x.len>y.len;
}
bool cmp(node x,node y)
{
	return x.k<y.k;
}
int T,n,m;
int k[N],l[N],r[N];
int main()
{
	int i,j;
	scanf("%d",&T);
	while(T--)
	{
		m=0;
		LL ans=0;
		scanf("%d",&n);
		for(i=1;i<=n;i++)
		{
			scanf("%d%d%d",&k[i],&l[i],&r[i]);
			if(l[i]>=r[i])
			{
				ans+=r[i];
				p[++m].k=k[i];
				p[m].len=l[i]-r[i];
			}
		}
		sort(p+1,p+m+1,cmp);
		int size=0;
		for(i=1;i<=m;i++)
		{
			q.push(p[i]);
			size++;
			if(size>p[i].k)
			{
				size--;
				q.pop();
			}
		}
		while(!q.empty())
		{
			ans+=q.top().len;
			q.pop();
		}
		j=0;
		for(i=1;i<=n;i++)
		{
			if(l[i]<r[i])
			{
				ans+=l[i];
				p[++j].len=r[i]-l[i];
				p[j].k=n-k[i];
			}
		}
		sort(p+1,p+j+1,cmp);
		size=0;
		for(i=1;i<=j;i++)
		{
			q.push(p[i]);
			size++;
			if(size>p[i].k)
			{
				size--;
				q.pop();
			}
		}
		while(!q.empty())
		{
			ans+=q.top().len;
			q.pop();
		}
		printf("%lld\n",ans);
	}
	return 0;
}