#include<bits/stdc++.h>
using namespace std;
#include<ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
#define ll long long

template<class T> ostream& operator<<(ostream &os,vector<T> V){
	os<<"[ ";for(auto v:V)os<<v<<" ";return os<<" ]";
}
template<class L,class R> ostream& operator<<(ostream &os,pair<L,R> P){
	return os<<"("<<P.first<<","<<P.second<<")";
}
ll mxm(){return LLONG_MIN;}
template<typename... Args>
ll mxm(ll a,Args... args){return max(a,mxm(args...));}
ll mnm(){return LLONG_MAX;}
template<typename... Args>
ll mnm(ll a,Args... args){return min(a,mnm(args...));}

#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__,__VA_ARGS__)
template<typename Arg1>
void __f(const char* name,Arg1&& arg1){
	cout<<name<<" : "<<arg1<<endl;
}
template<typename Arg1,typename... Args>
void __f(const char* names,Arg1&& arg1,Args&&... args){
		const char* comma=strchr(names+1,',');cout.write(names,comma-names)<<" : "<<arg1<<" | ";__f(comma+1,args...);
}
#else
#define trace(...) 1
#endif

#define ld long double
#define vll vector<ll>
#define pll pair<ll,ll>
#define ii pair<int,int>
#define vi vector<int>
#define vpll vector<pll>
#define vii vector<ii>
#define vvi vector<vi>
#define vvll vector<vll>
#define vvpll vector<vpll>
#define vvii vector<vii>
#define vld vector<ld>
#define vvld vector<vld>
#define I insert
#define F first
#define S second
#define pb push_back
#define all(x) x.begin(),x.end()
#define endl "\n"
#define siz(a)	((int)(a).size())

const int mod=1e9+7;
inline int add(int a,int b){a+=b;if(a>=mod)a-=mod;return a;}
inline int sub(int a,int b){a-=b;if(a<0)a+=mod;return a;}
inline int mul(int a,int b){return (a*1ll*b)%mod;}
inline int power(int a,int b){int rt=1;while(b>0){if(b&1)rt=mul(rt,a);a=mul(a,a);b>>=1;}return rt;}
inline int inv(int a){return power(a,mod-2);}

const int N=2e5+5;
ll dp[N][10],price[N][4],mx[N][4];
vvll costs(4);
int main()
{
	ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);cout<<setprecision(25);
	ll n;cin>>n;
	for(ll i=0;i<n;i++){
		for(ll j=1;j<=3;j++)costs[j].clear();
		ll k;cin>>k;
		while(k--){
			ll c,d;cin>>c>>d;costs[c].pb(d);
		}
		for(ll j=1;j<=3;j++)sort(all(costs[j])),reverse(all(costs[j]));
		if(costs[1].size()>=3)
			mx[i][3]=costs[1][0],price[i][3]=costs[1][0]+costs[1][1]+costs[1][2];
		if(costs[1].size()>=2)
			mx[i][2]=costs[1][0],price[i][2]=costs[1][0]+costs[1][1];
		if(costs[2].size()>=1&&costs[1].size()>=1)
			mx[i][2]=max(mx[i][2],costs[2][0]),price[i][2]=max(price[i][2],costs[1][0]+costs[2][0]);
		for(ll j=1;j<=3;j++)
			if(costs[j].size()>=1)
				mx[i][1]=max(mx[i][1],costs[j][0]);
		price[i][1]=mx[i][1];
	}
	for(ll j=1;j<=3;j++)dp[0][j]=price[0][j];
	for(ll i=1;i<n;i++){
		for(ll j=0;j<=3;j++){
			if(j>0&&price[i][j]==0)continue;
			for(ll p=0;p<10;p++){
				if(p>0&&dp[i-1][p]==0)continue;
				ll a=p,b=(p+j)%10;
				dp[i][b]=max(dp[i][b],dp[i-1][p]+price[i][j]+(b<a?mx[i][j]:0));
			}
		}
	}
	ll ans=0;for(ll i=0;i<10;i++)ans=max(ans,dp[n-1][i]);
	cout<<ans<<endl;
}