DEBUG = False

N = int(input())

crane_prices = {} # (idx, [(city, price)]
for n in range(N):
    list_len = int(input())

    for i in range(list_len):
        crane_id, price = [int(x) for x in input().split(' ')]

        if crane_id not in crane_prices.keys():
            crane_prices[crane_id] = [(n, price)]
        else:
            crane_prices[crane_id].append((n, price))

if DEBUG:
    print("crane_prices: ")
    print(crane_prices)

graph = []
best_costs = [0 for n in range(N)]

for i in range(N):
    graph.append([])

for key in crane_prices.keys():
    val = crane_prices[key]
    # print(key, val)

    for i, (city_id, buy_price) in enumerate(val):

        for to_idx in range(i+1, len(val)):
            to_city_id, sell_price = val[to_idx]
            profit = sell_price - buy_price

            if profit > 0:
                graph[city_id].append((to_city_id, profit))

if DEBUG:
    print(graph)

for i in range(N-1):
    curr_cost = best_costs[i]
    for e in graph[i]: # current city
        city_id, price = e
        best_costs[city_id] = max(best_costs[city_id], curr_cost + price)

    best_costs[i+1] = max(best_costs[i+1], best_costs[i])

if DEBUG:
    print("best_costs: ", best_costs)

print(max(best_costs))
