from operator import itemgetter
from heapq import heappush, heappop

T = int(input())

def solve():
	n = int(input())
	pos = []
	diff = []
	value = []

	pos = []
	neg = []	
	a = []

	for i in range(n):
		x, y, z = map(int, input().split())
	
		if y > z: 
			pos.append((x, y - z, i))	
		else:
			neg.append((x, z - y, i))

		a.append((x, y, z))

	pos = sorted(pos, key = lambda x:[n - x[0], x[1]])
	neg = sorted(neg, key = lambda x:[x[0], x[1]])

	position = len(pos) - 1
	idx = 0

	heap = []

	pos_good = []


	while position >= 0:
		while pos[idx][0] == position and idx < len(pos):
			heappush((-pos[idx][1], pos[idx][2]))
			idx += 1

		best = heappop(heap)
		pos_good.append(best[2])
		position -= 1

	heap = []
	neg_good = []

	idx = 0
	position = len(pos)

	while position < n:
		while neg[idx][0] == position and idx < len(neg):
			heappush((-neg[idx][1], neg[idx][2]))
			idx += 1

		best = heappop(heap)
		neg_good.append(best[2])
		position += 1

	pos_good = set(pos_good)
	neg_good = set(neg_good)

	result = 0

	for i in range(n):
		x, y, z = a[i]
		if y > z:
			if i in pos_good:
				result += y

			else:
				result += z

		else:
			if i in neg_good:
				result += z
			else:
				result += y

	print(result)

for t in range(T):
	solve()