
turns = int(input())
totalDamage = 0
cardCounter = 0
for turn in range(turns):
	cards = int(input())
	ratioArr = {}; costArr = []; damageArr = []
	for card in range(cards):
		cost, damage = list(map(int,input().split()))
		costArr.append(cost)
		damageArr.append(damage)
		ratioArr[card] = (damage/cost)
	# print(ratioArr)
	sortedRatioArr = sorted(ratioArr.items(), key=lambda kv: kv[1], reverse=True)
	# print(sortedRatioArr)
	totalCost = 0
	ratioIdx = 0
	for ratioIdx in range(cards):
		idx = sortedRatioArr[ratioIdx][0]
		totalCost += costArr[idx]
		if totalCost<=3:
			if (cardCounter+1)%10==0:
				totalDamage += 2*damageArr[idx]
			else:
				totalDamage += damageArr[idx]
			cardCounter+=1
		else:
			totalCost-= costArr[idx]
	print(totalDamage)

