#include<bits/stdc++.h>
#define INF 1000010000
#define nl '\n'
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define fi first
#define se second
#define pii pair<int,int>
#define pdd pair<double,double>
#define all(c) (c).begin(), (c).end()
#define SORT(c) sort(all(c))
#define sz(c) (c).size()
#define rep(i,n) for( int i = 0; i < n; ++i )
#define repi(i,n) for( int i = 1 ; i <= n; ++i )
#define repn(i,n) for( int i = n - 1 ; i >= 0 ; --i )
#define repf(j,i,n) for( int j = i ; j < n ; ++j )
#define die(s) {std::cout << s << nl;}
#define dier(s) {std::cout << s; return 0;}
#define dbg(var) {std::cout << #var << " = " << var << nl;}
#define vi vector<int>
typedef long long ll;
using namespace std;
constexpr int MAXN = 2e5 + 15;
ll dp[MAXN][11] = { -1LL };
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.precision(0);
int n;
cin >> n;
vector<vector<pii>> turns(n);
rep(i , n)
{
int k;
cin >> k;
rep(j , k)
{
int c , d;
cin >> c >> d;
turns[i].emplace_back(c , d);
}
}
rep(i , n)
{
map<int , vi> cur;
for(auto& el : turns[i]) cur[el.fi].pb(el.se);
for(auto& el : cur) sort(all(el.se) , std::greater<int>());
rep(j , 10) dp[i][j] = -1LL;
dp[i][0] = 0LL;
static const vector<vi> ways =
{ // 1 2 3
{ 1 , 0 , 0 },
{ 0 , 1 , 0 },
{ 0 , 0 , 1 },
{ 2 , 0 , 0 },
{ 1 , 1 , 0 },
{ 3 , 0 , 0 },
{ 0 , 0 , 0 }
};
for(auto& way : ways)
{
vector<ll> d;
bool good = true;
rep(j , 3)
{
if(cur[1 + j].size() < way[j])
{
good = false;
break;
}
rep(l , way[j])
{
d.pb(cur[1 + j][l]);
}
}
if(!good) continue;
sort(all(d) , std::greater<ll>());
const int cards = accumulate(all(way) , 0);
rep(j , 10)
{
dp[i][cards] = max(dp[i][cards] , accumulate(all(d) , 0LL));
if(i > 0 && dp[i - 1][j] != -1)
{
auto dd = d;
if(j + cards >= 10) dd[0] *= 2;
dp[i][(j + cards) % 10] = max(dp[i][(j + cards) % 10] , dp[i - 1][j] + accumulate(all(dd) , 0LL));
}
}
}
}
ll ans = 0;
rep(i , n)
{
rep(j , 10)
{
ans = max(ans , dp[i][j]);
}
}
cout << ans << nl;
return 0;
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KCiNkZWZpbmUgSU5GIDEwMDAwMTAwMDAKI2RlZmluZSBubCAnXG4nCiNkZWZpbmUgcGIgcHVzaF9iYWNrCiNkZWZpbmUgcHBiIHBvcF9iYWNrCiNkZWZpbmUgbXAgbWFrZV9wYWlyCiNkZWZpbmUgZmkgZmlyc3QKI2RlZmluZSBzZSBzZWNvbmQKI2RlZmluZSBwaWkgcGFpcjxpbnQsaW50PgojZGVmaW5lIHBkZCBwYWlyPGRvdWJsZSxkb3VibGU+CiNkZWZpbmUgYWxsKGMpIChjKS5iZWdpbigpLCAoYykuZW5kKCkKI2RlZmluZSBTT1JUKGMpIHNvcnQoYWxsKGMpKQojZGVmaW5lIHN6KGMpIChjKS5zaXplKCkKI2RlZmluZSByZXAoaSxuKSBmb3IoIGludCBpID0gMDsgaSA8IG47ICsraSApCiNkZWZpbmUgcmVwaShpLG4pIGZvciggaW50IGkgPSAxIDsgaSA8PSBuOyArK2kgKQojZGVmaW5lIHJlcG4oaSxuKSBmb3IoIGludCBpID0gbiAtIDEgOyBpID49IDAgOyAtLWkgKQojZGVmaW5lIHJlcGYoaixpLG4pIGZvciggaW50IGogPSBpIDsgaiA8IG4gOyArK2ogKQojZGVmaW5lIGRpZShzKSB7c3RkOjpjb3V0IDw8IHMgPDwgbmw7fQojZGVmaW5lIGRpZXIocykge3N0ZDo6Y291dCA8PCBzOyByZXR1cm4gMDt9CiNkZWZpbmUgZGJnKHZhcikge3N0ZDo6Y291dCA8PCAjdmFyIDw8ICIgID0gIiA8PCB2YXIgPDwgbmw7fQojZGVmaW5lIHZpIHZlY3RvcjxpbnQ+CnR5cGVkZWYgbG9uZyBsb25nIGxsOwoK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