#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector> 
#define LL long long
using namespace std;
const int N=2e5+10;
struct NODE{
	int k,l,r;
	bool operator <(const NODE &other)
	const { return k<other.k; }
}node[N];
priority_queue<int,vector<int>,greater<int> >que;//小根堆,注意空格！！！！ 
int main()
{
	int T,n;
	scanf("%d",&T);
	while(T)
	{
		T--;
		LL sum=0;
		LL now=0;
		int cnt=0; 
		scanf("%d",&n);
		for(int i=1;i<=n;i++)
		{
			scanf("%d%d%d",&node[i].k,&node[i].l,&node[i].r);
			now+=max(node[i].l,node[i].r);
		}
		sort(node+1,node+n+1);
		int j=1;
		for(int i=1;i<=n;i++)
		{
			while(j<=n&&node[j].k<=i)
			{
				if(node[j].l>=node[j].r) //注意等于的情况不要漏掉
				{
					sum+=node[j].r;
					que.push(node[j].l-node[j].r);
				}
				j++;
			}
			while(que.size()>i)	que.pop();
		}
		while(que.size())
		{
			sum+=que.top();
			cnt++;
			que.pop();
		}
		j=n;
		for(int i=n;i>0;i--)
		{
			while(j&&node[j].k>=i)
			{
				if(node[j].r>node[j].l)
				{
					sum+=node[j].l;
					que.push(node[j].r-node[j].l);
				}
				j--;
			}
			while(que.size()>n-i)	que.pop();
		}
		while(que.size())
		{
			sum+=que.top();
			cnt++;
			que.pop();
		}
		cout<<sum<<endl;
	}
	return 0;
}

