T = int(input())
for fakevar in range(T):
	N=int(input())
	p=[]
	n=[]
	min_p=0
	min_n=0
	for fakevar2 in range(N):
		K,L,R=map(int, input().split())
		if L-R>=0:
			p+=[[L-R,K-1]]
			min_p+=R
		else:
			n+=[[R-L,N-K-1]]
			min_n+=L
	
	p.sort(key=lambda x: x[0], reverse=True)
	n.sort(key=lambda x: x[0], reverse=True)
	p_set=set([])
	n_set=set([])
	p_score=0
	n_score=0
	for i in range(len(p)):
		j=min(p[i][1],len(p)-1)
		while j in p_set and j>=0:
			j+=(-1)
		
		if j>=0:
			p_set.add(j)
			p_score+=p[i][0]
	
	for i in range(len(n)):
		j=min(n[i][1],len(n)-1)
		while j in n_set and j>=0:
			j+=(-1)
		
		if j>=0:
			n_set.add(j)
			n_score+=n[i][0]
	
	print(p_score+n_score+min_p+min_n)

		





# 2darray [[0] * 4 for i in range(3)]