

# point update
# range query
class SegmentTreeNode:
	def __init__(self, start, end, initial, merge):
#		print("s", start, end)
		self.start = start
		self.end = end
		self.width = end - start
		self.merge = merge
		self.initial = initial
		if self.width > 1:
			w2 = self.width // 2
			self.left = SegmentTreeNode(start, start + w2, initial, merge)
			self.right = SegmentTreeNode(start + w2, end, initial, merge)
			self.value = self.merge(self.left.value, self.right.value)
		else:
			self.value = self.initial

	def update(self, index, value):
		if self.start <= index and index < self.end:
			if self.width == 1:
				self.value = value
			else:
				self.left.update(index, value)
				self.right.update(index, value)
				self.value = self.merge(self.left.value, self.right.value)
	
	def pick(self, start, end):
		if start <= self.start and self.end <= end:
			return self.value
		elif self.start < end and self.end > start:
#			m = self.merge(self.left.pick(start, end), self.right.pick(start, end))
#			print(start, end, m)
			return self.merge(self.left.pick(start, end), self.right.pick(start, end))
		else:
			return self.initial
	
	def to_list(self, items):
		if self.width == 1:
			items.append(self.value)
		else:
			self.left.to_list(items)
			self.right.to_list(items)
	
class SegmentTree:
	def __init__(self, n, *, merge, initial = None):
		width = 1
		while width < n:
			width *= 2
		self.root = SegmentTreeNode(0, width, initial, merge)
		self.root.value = 0
		self.capacity = width
	
	def update(self, index, value):
		self.root.update(index, value)
	
	def pick(self, start, end):
		return self.root.pick(start, end)
	
	def to_list(self):
		items = []
		self.root.to_list(items)
		return items
	

def merge_r(l, r):
	if r == None:
		return l
	elif l == None:
		return r
	elif r[1] == None:
		return r
	else:
		return l

def merge_l(l, r):
	if r == None:
		return l
	elif l == None:
		return r
	elif l[1] == None:
		return l
	else:
		return r

T = int(input())

for t in range(T):
	N = int(input())
	
	camels = []
	
	for _ in range(N):
		K, L, R = map(int, input().split())
		camels.append((abs(L - R), L, R, K))
	
	camels.sort()
	camels.reverse()
	
	width = 1
	while width < N:
		width *= 2
	st_l = SegmentTree(N, merge = merge_l)
	st_r = SegmentTree(N, merge = merge_r)
	
	for i in range(N):
		st_l.update(i, (i, None))
		st_r.update(i, (i, None))
	
	for i in range(N + 1, width):
		st_l.update(i, (i, -1))
		st_r.update(i, (i, -1))
	
	total = 0
	for D, L, R, K in camels:
		if L > R:
			i, c = st_r.pick(0, K)
#			print(L, R, i)
			if c == None:
#				print("x", i, R)
				st_r.update(i, (i, c))
				st_l.update(i, (i, c))
				total += L
			else:
				total += R
		else:
			pair = st_l.pick(K, N)
			if pair == None:
				total += L
			else:
				i, c = pair
#				print(L, R, i)
				if c == None:
#					print("x", i, R)
					st_r.update(i, (i, c))
					st_l.update(i, (i, c))
					total += R
				else:
					total += L
	print(total)
