n = int(input())
cards = []

for _ in range(n):
	i = int(input())
	cards.append([] * i)
	for _ in range(i):
		cards[-1].append([*map(int, input().split())])
	cards[-1].sort(key=lambda x: (-x[1], x[0]))

ans = to = 0

for i in cards:
	t = 0
	for j in i:
		if t + j[0] <= 3:
			to += 1
			ans += j[1] * 2 if to % 10 == 0 else j[1]
			t += j[0]

print(ans)