#pragma GCC optimize("Ofast,fast-math,unroll-loops")
//#pragma GCC tartget("avx,abm,sse,sse2,sse3,ssse3,sse4,popcnt,tune=native")
//#pragma comment(linker, "/STACK:45000000")
 
#include <bits/stdc++.h>
 
#define int ll
//#define double long double
//#define endl '\n'
#define all(C) (C).begin(), (C).end()
#define rall(C) (C).rbegin(), (C).rend()
#define mp make_pair 
#define pb emplace_back
#define dbg(x) cerr << #x << " : " << x << endl
#define PI 3.141592653589
 
using namespace std;
 
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using ld = long double;
using pld = pair <ld, ld>;
 
/* 
const int MAX_MEM = 4e7;
char MEM[MAX_MEM];
int MEM_POS = 0;
void* operator new(size_t x)
{
    auto ret = MEM + MEM_POS;
    MEM_POS += x;
    assert(MEM_POS < MAX_MEM);
    return ret;
}
void operator delete(void*){}
*/
 
template <class T>
istream& operator>> (istream &in, vector <T> &a)
{
    for (auto &i : a)
        in >> i;
    return in;
}
 
template <class T> 
ostream& operator<< (ostream &out, vector <T> a)
{
    for (auto &i : a)
        out << i << ' ';
    return out;
}
 
template <class T, class U>
istream& operator>> (istream &in, pair <T, U> &p)
{
    in >> p.first >> p.second;
    return in;
}
 
template <class T, class U>
ostream& operator<< (ostream &out, pair <T, U> p)
{
    out << p.first << " " << p.second << " ";
    return out;
}
 
inline void Start()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    //freopen("put.in", "r", stdin);
    //freopen("put.out", "w", stdout);
}
template <class T>
T gcd(T a, T b)
{
    return a ? gcd(b % a, a) : b;
}

signed main()
{
    Start();
    int n;
    cin >> n;
    vector <vector <int>> dp(n, vector <int> (10, -1000000000000000000ll));
    for (int i = 0; i < n; ++i)
    {
        int k;
        cin >> k;
        vector <pii> a(k);
        cin >> a;
        // try 3
        sort(all(a));
        int j = 0;
        while (j < k && a[j].first == 1)
            ++j;
        --j;
        if (j >= 2)
        {
            for (int d = 0; d < 10; ++d)
            {
                dp[i][d] = max(dp[i][d], (i ? dp[i - 1][(d - 3 + 10) % 10] : d == 3 ? 0 : -1000000000000000000ll) + a[j - 2].second + a[j - 1].second + (d < 3 ? 2 : 1) * a[j].second);
            }
        }
        // try 2
        j = 0;
        while (j < k && a[j].first < 3)
            ++j;
        sort(a.begin(), a.begin() + j, [] (pii a, pii b)
        {
            return a.second < b.second;
        });
        --j;
        vector <vector <int>> kek(3);
        for (auto &[c, d] : a)
            kek[c - 1].pb(d);
        sort(all(kek[0]));
        sort(all(kek[1]));
        int sz1 = kek[0].size(), sz2 = kek[1].size();
        int fs, sc;
        {
            for (int d = 0; d < 10; ++d)
            {
                if (sz1 >= 2)
                dp[i][d] = max(dp[i][d],
                               (i ? dp[i - 1][(d - 2 + 10) % 10] : d == 2 ? 0 : -1000000000000000000ll) + kek[0][sz1 - 2] + (d < 2 ? 2 : 1) * kek[0][sz1 - 1]);
                if (sz1 >= 1 && sz2 >= 1)
                {

                    dp[i][d] = max(dp[i][d],
                               (i ? dp[i - 1][(d - 2 + 10) % 10] : d == 2 ? 0 : -1000000000000000000ll) + min(kek[0][sz1 - 1], kek[1][sz2 - 1]) + (d < 2 ? 2 : 1) * max(kek[0][sz1 - 1], kek[1][sz2 - 1]));
                }
 
            }
        }
        // try 1
        sort(all(a), [] (pii a, pii b)
        {
            return a.second < b.second;
        });
        for (int d = 0; d < 10; ++d)
        {
            dp[i][d] = max(dp[i][d], (i ? dp[i - 1][(d - 1 + 10) % 10] : d == 1 ? 0 : -1000000000000000000ll) +
                           (d < 1 ? 2 : 1) * a[k - 1].second);
        }
    }
    cout << *max_element(all(dp[n - 1]));
    return 0;
}

