#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <complex>
#define ll long long
#define ld long double
#define F first
#define S second
#define Tsetso ios_base::sync_with_stdio(0) ; cin.tie(0) ;
#define pi acos(-1)
#define X real()
#define Y imag()
#define angle(a) (atan2((a).imag(),(a).real()))
#define vec(a,b) ((b)-(a))
#define lenght(a) (hypot((a).imag(),(a).real()))
#define normalize(a) (a)/length(a)
#define point complex<long double>
#define dP(a,b)((conj(a)*(b)).real())
#define cP(a,b)((conj(a)*(b)).imag())
#define same(p1,p2)  (dP(vec(p1,p2),vec(p1,p2)) < EPS)
#define lengthsqr(p)   dP(p,p)
#define rotate0(p,ang)  ((p)*exp(point(0,ang)))
#define rotataA(p,ang,about) (rotate0(vec(about,p),ang)+about)
#define reflect0(v,m) (conj((v)/(m))*(m))
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
const ld EPS = (1e-10);
using namespace std;
using namespace __gnu_pbds;
const ll N = 2e5+50, inf = 1e3+5, mod = 1e9+7, NN = 1e5+10;
ll dp[N][10],n;
vector < ll > v1[N],v2[N],v3[N] ;
ll solve ( int idx, int mod)
{
    if ( idx == n )
        return 0 ;
    if ( dp[idx][mod] != -1)
        return dp[idx][mod];
    ll res = 0 ,mx1 = 0, mx2 = 0, mx3 = 0 ;
    if (v1[idx].size())
        mx1 = v1[idx][0];
    if (v2[idx].size())
        mx1 = max(v2[idx][0],mx1);
    if (v3[idx].size())
        mx1 = max(v3[idx][0],mx1);
    if ( mod == 9)
        mx1 *= 2 ;
    if (v1[idx].size() >= 1 )
    {
        ll aa = 0, bb = 0, cc = 0;
        aa = v1[idx][0];
        if ( v1[idx].size() >= 2)
        {
            bb = v1[idx][1];
            mx2 = aa+bb;
            if ( mod >= 8)
                mx2 += aa;
        }
        if ( v2[idx].size())
        {
            bb = v2[idx][0];
            cc = aa+bb;
            if ( mod >= 8)
                cc += max(aa,bb);
            mx2 = max(cc,mx2);
        }
    }
    if (v1[idx].size() >= 3 )
    {
        ll aa, bb,cc;
        aa = v1[idx][0];
        bb = v1[idx][1];
        cc = v1[idx][2];
        mx3 = aa+bb+cc;
        if ( mod >= 7)
            mx3 += aa;
    }
        res = solve(idx+1,mod);
    if (mx1)
        res = res=max(res,mx1+solve(idx+1,(mod+1)%10));
    if (mx2)
        res = max(res,mx2+solve(idx+1,(mod+2)%10));
    if (mx3)
        res = max(res,mx3+solve(idx+1,(mod+3)%10));
    return dp[idx][mod] = res;
}
int main()
{
    Tsetso
    cin >> n;
    ll c, d ;
    int k ;
    for ( int i = 0 ; i < n ; i++)
    {
        cin >> k ;
        while (k--)
        {
            cin >> c >> d ;
            if ( c == 1)
                v1[i].push_back(d);
            else if ( c == 2)
                v2[i].push_back(d);
            else
                v3[i].push_back(d);
        }
        if ( v1[i].size())
            sort(v1[i].rbegin(),v1[i].rend());
        if ( v2[i].size())
            sort(v2[i].rbegin(),v2[i].rend());
        if ( v2[i].size())
            sort(v3[i].rbegin(),v3[i].rend());
    }
    memset(dp,-1,sizeof dp);
    cout << solve(0,0);
}
