parseInt(x)=parse(Int, x)
parseMap(x::Array{SubString{String},1})=map(parseInt, x)
function eval(a::Tuple{Int,Int,Int},b::Tuple{Int,Int,Int})
	if a[2]-a[3]<b[2]-b[3]
		1
	elseif a[2]-a[3]==b[2]-b[3]
		0
	else
		-1
	end
end

function hpush(h::Array{Tuple{Int,Int,Int},1},x::Tuple{Int,Int,Int})
	push!(h,x)
	if length(h) > 1
		l = length(h)
		k = l>>1
		while eval(x,h[k])==1&&k>=1
			tmp = h[k]
			h[k] = x
			h[l] = tmp
			k = max(k>>1,1)
			l = l>>1
		end
	end
end

function hpop(h::Array{Tuple{Int,Int,Int},1})
	x = h[1]
	z = pop!(h)
	if !isempty(h)
		h[1] = z
		k = 1
		l = length(h)
		while k <= l
			if 2*k>l
				break
			elseif 2*k+1>l
				if eval(z,h[2*k])>=0
					break
				else
					tmp = h[2*k]
					h[2*k] = z
					h[k] = tmp
					k = 2*k
				end
			else
				if eval(z,h[2*k])>=0&&eval(z,h[2*k+1])>=0
					break
				else
					t = eval(h[2*k],h[2*k+1])>=0 ? 2*k : 2*k+1
					tmp = h[t]
					h[t] = z
					h[k] = tmp
					k = t
				end
			end
		end
	end
	x
end


function main()
	t = readline() |> parseInt
	for iter in 1:t
		n = readline() |> parseInt
		a = Tuple{Int,Int,Int}[]
		b = Tuple{Int,Int,Int}[]
		for i in 1:n
			k,l,r = readline() |> split |> parseMap
			if l>r
				push!(a,(k,l,r))
			else
				push!(b,(k,r,l))
			end
		end
		a = sort(a,by=x->x[1])
		b = sort(b,by=x->x[1],rev=true)
		h = Tuple{Int,Int,Int}[]
		s = 0
		ct = 0
		for i in a
			if ct<i[1]
				s += i[2]
				ct += 1
			else
				if length(h)==0
					s += i[3]
					hpush(h,i)
				else
					j = hpop(h)
					if i[2]-i[3]>j[2]-j[3]
						s += i[2]-j[2]+j[3]
						hpush(h,j)
					else
						s += j[2]-j[3]+i[3]
						hpush(h,i)
					end
				end
			end
		end
		ct = 0
		h = Tuple{Int,Int,Int}[]
		for i in b
			if ct<n-i[1]
				s += i[2]
				ct += 1
			else
				if length(h)==0
					s += i[3]
					hpush(h,i)
				else
					j = hpop(h)
					if i[2]-i[3]>j[2]-j[3]
						s += i[2]-j[2]+j[3]
						hpush(h,j)
					else
						s += j[2]-j[3]+i[3]
						hpush(h,i)
					end
				end
			end
		end
		println(s)
	end
end

main()