#include<bits/stdc++.h>
#include<stdio.h>
using namespace std;
typedef long long int ld;
typedef unsigned long long int lu;
typedef vector< int > vi;
typedef vector < ld > vd;
typedef set <ld> sd;
typedef vector< vi > vvi;
typedef vector< vd > vvd;
typedef pair< int,int > ii;
typedef pair< ld,ld > id;
#define sz(a) ld((a).size())
#define pb push_back
#define all(c) (c).begin(),(c).end()
#define tr(c,i) for(__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define present(c,x) ((c).find(x) != (c).end())
#define cpresent(c,x) (find(all(c),x) != (c).end())
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define Vi vector
#define pr pair
#define mpr make_pair
#define isnum(a) (a>=48 && a<=57)
#define isS(a) (a>='a' && a<='z')
#define isU(a) (a>='A' && a<='Z')
#define toS(a) (isU(a)?a+32:a)
#define toU(a) (isS(a)?a-32:a)
#define toC(a) a&15

set <ld> s[100005];
ld d[100005];

vector<char> prime (100000+1, true);
void sieve(ld n)
{	
	//O(nloglogn) time
	prime[0] = prime[1] = false;
	for (ld i=2; i<=n; ++i)
	{
		if (prime[i])
		{
			s[i].insert(i);			
			for (ld j=2*i; j<=n; j+=i)
			{
				prime[j] = false;
				s[j].insert(i);
			}
		}
	}
}
#define rep(i,a,b) for(i=a;i<b;i++)
#define REP(i,a,b) for(i=a;i<=b;i++)
#define reprev(i,a,b) for(i=a;i>=b;i--)

void init(ld ar[], ld n, ld val)
{
	ld i;
	rep(i,0,n)
	{
		ar[i]=val;
	}
}

ld search1(vector<ld> &v, ld f)
{
	ld left=0;
	ld right=v.size()-1;
	while(left<right)
	{
		ld mid = left + (right-left+1)/2;
		if(v[mid]<=f)
		{
			left=mid;
		}
		else right=mid-1;
	}
	return left;
}

int main()
{
	ld n,k,i,j;
	cin >> n;
	ld dp[n+1][10]={0};
	bool br[10]={0};
	ld t = n;
	REP(i,1,n)
	{
		cin >> k;
		ld c1,d;
		ld a1=0,a2=0,a3=0,b=0,c=0;
		for(j=0;j<k;j++)
		{
			cin >> c1 >> d;
			if(c1==2){b = max(b,d);}
			else if(c1==3){c=max(c,d);}
			else 
			{
				if(d>=a1)
				{
					a3=a2;
					a2=a1;
					a1=d;
				}
				else if(d>=a2){a3=a2;a2=d;}
				else if(d>=a3){a3=d;}
			}
		}
		ld maxs=max(a1,b);
		ld mins=min(a1,b);
		if(i==1)
		{
			if(c!=0){dp[1][1]=c;br[1]=1;}
			if(a1!=0){dp[1][1]=max(dp[1][1],a1);br[1]=1;}
			if(b!=0){dp[1][1]=max(dp[1][1],b);br[1]=1;}
			if(a1!=0 && b!=0){dp[1][2]=maxs+mins;br[2]=1;}
			if(a2!=0){dp[1][2]=a1+a2;br[2]=1;}
			if(a3!=0){dp[1][3]=a1+a2+a3;br[3]=1;}
			//br[0]=1;
			continue;
		}
		vd v;
		for(j=3;j<10;j++)
		{
			if(br[j])dp[i][j]=max(dp[i][j],dp[i-1][j]);
			if(br[j-3] && a3!=0)
			{
				if(!br[j])v.pb(j);
				dp[i][j]=max(dp[i][j],dp[i-1][j-3]+a1+a2+a3);
			}
			if(br[j-2])
			{
				if(maxs!=0 && mins!=0)dp[i][j]=max(dp[i][j],dp[i-1][(j-2+10)%10]+maxs+mins);
				if(a1!=0 && a2!=0)dp[i][j]=max(dp[i][j],dp[i-1][(j-2+10)%10]+a1+a2);
				if(dp[i][j]!=0)v.pb(j);
			}
			if(br[j-1])
			{
				if(c!=0)dp[i][j]=max(dp[i][j],dp[i-1][(j-1+10)%10]+c);
				if(maxs!=0)dp[i][j]=max(dp[i][j],dp[i-1][(j-1+10)%10]+maxs);
				if(a1!=0)dp[i][j]=max(dp[i][j],dp[i-1][(j-1+10)%10]+a1);
				if(dp[i][j]!=0)v.pb(j);
			}
		}
		j=2;
		if(br[2])dp[i][2]=max(dp[i][2],dp[i-1][2]);
		if(br[9] && a3!=0)
		{
			if(!br[2])v.pb(2);
			dp[i][2]=max(dp[i][2],dp[i-1][9]+2*a1+a2+a3);
		}
		if(br[j-2])
		{
			if(maxs!=0 && mins!=0)dp[i][j]=max(dp[i][j],dp[i-1][(j-2+10)%10]+maxs+mins);
			if(a1!=0 && a2!=0)dp[i][j]=max(dp[i][j],dp[i-1][(j-2+10)%10]+a1+a2);
			if(dp[i][j]!=0)v.pb(j);
		}
		if(br[j-1])
		{
			if(c!=0)dp[i][j]=max(dp[i][j],dp[i-1][(j-1+10)%10]+c);
			if(maxs!=0)dp[i][j]=max(dp[i][j],dp[i-1][(j-1+10)%10]+maxs);
			if(a1!=0)dp[i][j]=max(dp[i][j],dp[i-1][(j-1+10)%10]+a1);
			if(dp[i][j]!=0)v.pb(j);
		}
		j=1;
		if(br[1])dp[i][1]=max(dp[i][1],dp[i-1][1]);
		if(br[8] && a3!=0)
		{
			if(!br[j])v.pb(j);
			dp[i][1]=max(dp[i][1],dp[i-1][8]+2*a1+a2+a3);
		}
		if(br[9] && a1!=0 && b!=0)
		{
			if(maxs!=0 && mins!=0)dp[i][j]=max(dp[i][j],dp[i-1][(j-2+10)%10]+2*maxs+mins);
			if(a1!=0 && a2!=0)dp[i][j]=max(dp[i][j],dp[i-1][(j-2+10)%10]+2*a1+a2);
			if(dp[i][j]!=0)v.pb(j);
		}
		if(br[j-1])
		{
			if(c!=0)dp[i][j]=max(dp[i][j],dp[i-1][(j-1+10)%10]+c);
			if(maxs!=0)dp[i][j]=max(dp[i][j],dp[i-1][(j-1+10)%10]+maxs);
			if(a1!=0)dp[i][j]=max(dp[i][j],dp[i-1][(j-1+10)%10]+a1);
			if(dp[i][j]!=0)v.pb(j);
		}

		j=0;
		if(br[0])dp[i][0]=max(dp[i][0],dp[i-1][0]);
		if(br[(j-3+10)%10] && a3!=0)
		{
			if(!br[j])v.pb(j);
			dp[i][j]=max(dp[i][j],dp[i-1][(j-3+10)%10]+2*a1+a2+a3);
		}
		if(br[(j-2+10)%10] && a1!=0 && b!=0)
		{
			if(maxs!=0 && mins!=0)dp[i][j]=max(dp[i][j],dp[i-1][(j-2+10)%10]+2*maxs+mins);
			if(a1!=0 && a2!=0)dp[i][j]=max(dp[i][j],dp[i-1][(j-2+10)%10]+2*a1+a2);
			if(dp[i][j]!=0)v.pb(j);
		}
		if(br[(j-1+10)%10])
		{
			//if(!br[j])v.pb(j);
			if(c!=0)dp[i][j]=max(dp[i][j],dp[i-1][(j-1+10)%10]+2*c);
			if(maxs!=0)dp[i][j]=max(dp[i][j],dp[i-1][(j-1+10)%10]+2*maxs);
			if(a1!=0)dp[i][j]=max(dp[i][j],dp[i-1][(j-1+10)%10]+2*a1);
			if(dp[i][j]!=0)v.pb(j);
		}
		for(ld x : v)br[x]=1;
	}
	cout << *max_element(dp[n],dp[n]+10) << endl;
	return 0;
}