import sys
input=sys.stdin.readline
from heapq import heappop,heappush
t=int(input())
for _ in range(t):
	happy=0
	n=int(input())
	camel=[]
	for i in range(n):
		k,l,r=map(int,input().split())
		happy+=r
		if l-r>0:
			plus=1
		else:
			plus=0
		camel.append((l-r,k,plus))
	camel.sort(key=lambda x:(-x[2],x[1]))
	# print(camel)#前のものほど早く使うべき
	for i in range(n):
		if (i+1)<=camel[i][1]:
			happy+=camel[i][0]
	print(happy)