#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 op1 = 0, op2 = 0, op3 = 0, op4 = 0, op5 = 0, op6 = 0;
	ll ans=solve(idx+1,mod);
	if(v1[idx].size() >= 3)
	{
		op1 = v1[idx][0] + v1[idx][1] + v1[idx][2] + solve(idx + 1, (mod + 3) % 10);
		if( mod >= 7)
			op1 += v1[idx][0];
	}
	if(!v2[idx].empty() && !v1[idx].empty())
	{
		op2 = v1[idx][0] + v2[idx][0] + solve(idx + 1, (mod + 2) % 10);
		if( mod >= 8 )
			op2 += max(v1[idx][0], v2[idx][0]);
	}
	if(!v2[idx].empty())
	{
		op3 = v2[idx][0] + solve(idx + 1, (mod + 1) % 10);
		if( mod  >= 9)
			op3 += v2[idx][0];
	}
	if(!v3[idx].empty())
	{
		op4 = v3[idx][0] + solve(idx + 1, (mod + 1) % 10);
		if( mod >= 9)
			op4 += v3[idx][0];
	}
	if(!v1[idx].empty())
	{
		op5 = v1[idx][0] + solve(idx + 1, (mod + 1) % 10);
		if( mod  >= 9)
			op5 += v1[idx][0] ;
	}
	if(v1[idx].size() >= 2)
	{
		op6 = v1[idx][0] + v1[idx][1] + solve(idx + 1, (mod + 2) % 10);
		if( mod >= 8)
			op6 += v1[idx][0];
	}
	ans = max(max(ans, op1), max(op2, op3));
	ans = max(max(ans, op4), max(op5, op6));
    return dp[idx][mod] = ans;
}
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);
}
