turns = int(input())
totalDamage = 0
cardCounter = 0
next10th = 9

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)

	tempTurnArr = []
	totalCost = 0
	ratioIdx = 0
	for ratioIdx in range(cards):
		idx = sortedRatioArr[ratioIdx][0]
		totalCost += costArr[idx]
		if totalCost<=3:
			tempTurnArr.append(damageArr[idx])
		else:
			totalCost-= costArr[idx]

	if (cardCounter+len(tempTurnArr)>next10th):
		artefactIdx = (next10th - cardCounter - 1)
		tempTurnArr[0] , tempTurnArr[artefactIdx] = tempTurnArr[artefactIdx] , tempTurnArr[0]
		tempTurnArr[artefactIdx] *= 2
		next10th+=10

	totalDamage += sum(tempTurnArr) 
	cardCounter+=len(tempTurnArr)
print(totalDamage)

			# if (cardCounter+1)%10==0:
			# 	totalDamage += 2*damageArr[idx]
			# else:
			# 	totalDamage += damageArr[idx]