#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

long long answer[2][10];

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    for (int i = 1; i < 10; ++i)
    {
        answer[1][i] = numeric_limits<long long>::min();
    }
    int n;
    cin >> n;
    for (int i = 0; i < n; ++i)
    {
        int cur = i & 1;
        int pre = !cur;
        copy(answer[pre], answer[pre] + 10, answer[cur]);
        long long ones[3] = { 0, 0, 0 };
        long long two = 0;
        long long three = 0;
        int k;
        cin >> k;
        for (int q = 0; q < k; ++q)
        {
            long long x, y;
            cin >> x >> y;
            if (x == 1)
            {
                if (y >= ones[0])
                {
                    ones[2] = ones[1];
                    ones[1] = ones[0];
                    ones[0] = y;
                }
                else if (y >= ones[1])
                {
                    ones[2] = ones[1];
                    ones[1] = y;
                }
                else if (y >= ones[2])
                {
                    ones[2] = y;
                }
            }
            else if (x == 2)
            {
                two = max(two, y);
            }
            else
            {
                three = max(three, y);
            }
        }
        long long one_card_max = max(ones[0], max(two, three));
        if (one_card_max > 0)
        {
            for (int z = 0; z < 10; ++z)
            {
                answer[cur][z] = max(answer[cur][z], one_card_max * (z == 0 ? 2 : 1) + answer[pre][(z + 9) % 10]);
            }
        }
        int two_candidates[2] = { ones[0], ones[1] };
        if (two > ones[1])
        {
            two_candidates[1] = two;
        }
        if (two_candidates[0] < two_candidates[1])
        {
            swap(two_candidates[0], two_candidates[1]);
        }
        if (two_candidates[1] != 0)
        {
            for (int z = 0; z < 10; ++z)
            {
                int scores = two_candidates[0] + two_candidates[1];
                if (z == 0 || z == 1)
                {
                    scores += two_candidates[0];
                }
                answer[cur][z] = max(answer[cur][z], answer[pre][(z + 8) % 10] + scores);
            }
        }

        if (ones[2] != 0)
        {
            for (int z = 0; z < 10; ++z)
            {
                int scores = ones[0] + ones[1] + ones[2];
                if (z == 0 || z == 1 || z == 2)
                {
                    scores += ones[0];
                }
                answer[cur][z] = max(answer[cur][z], answer[pre][(z + 7) % 10] + scores);
            }
        }
    }

    long long ans = 0;
    for (int i = 0; i < 10; ++i)
    {
        ans = max(ans, answer[(n - 1) & 1][i]);
    }
    cout << ans << endl;

    return 0;
}