#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization ("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i,a,b) for(ll i = (a); i < (b); i++)
#define iter(it,c) for(auto it = (c).begin(); it != (c).end();++it)
#define pb push_back
#define fs first
#define sc second
typedef pair<ll,ll> ii;
typedef vector<ll> vi;
typedef vector<ii> vii;
typedef vector<vi> vvi;
typedef vector<vii> vvii;

const ll INF = ~(1<<31);
const double pi = acos(-1);
const double EPS = 1e-9;
typedef unsigned long long ull;

template <class T> T smod(T a, T b) {
  return (a % b + b) % b; }

vvii A;
ll n;

ll calc(vi BS, ll flag) {
    ll sm = 0;
    iter(it,BS) sm += *it;
    sort(BS.rbegin(), BS.rend());
    if(flag) {
        sm += BS[0];
    }
    return sm;
}

ll sv(ll at, ll val, ll flag) {
    if(val == 0) return 0;
    vi F1;
    vi F2;
    vi F3;
    rep(i,0,size(A[at])) {
        if(A[at][i].fs == 1) F1.pb(A[at][i].sc);
        if(A[at][i].fs == 2) F2.pb(A[at][i].sc);
        if(A[at][i].fs == 3) F3.pb(A[at][i].sc);
    }
    sort(F1.rbegin(), F1.rend());
    sort(F2.rbegin(), F2.rend());
    sort(F3.rbegin(), F3.rend());
    ll sm = 0;
    if(val == 1) {
        if(size(F1) != 0) sm = max(sm, calc({F1[0]}, flag));
        if(size(F2) != 0) sm = max(sm, calc({F2[0]}, flag));
        if(size(F3) != 0) sm = max(sm, calc({F3[0]}, flag));
    } else if(val == 2) {
        if(size(F1) >= 2) sm = max(sm, calc({F1[0], F1[1]}, flag));
        if(size(F1) >= 1 && size(F2) >= 1) sm = max(sm, calc({F1[0], F2[0]}, flag));
    } else if(val == 3) {
        if(size(F1) >= 3) sm = max(sm, calc({F1[0], F1[1], F1[2]}, flag));
    }
    return sm;
}

ll memo[200'050][12];
ll dp(ll at, ll rmcnt) {
    if(at >= n) return 0;
    if(memo[at][rmcnt] != -1) return memo[at][rmcnt];
    ll mx = 0;
    rep(i,0,4) {
        ll s = sv(at,i,i+rmcnt >= 10);
        mx = max(mx, dp(at+1,(i+rmcnt)%10)+s);
    }
    return memo[at][rmcnt] = mx;
}

int main() {
    cin.sync_with_stdio(false);
    memset(memo,-1,sizeof(memo));
    cin >> n;
    rep(i,0,n) {
        ll k;
        cin >> k;
        vii B(k);
        rep(j,0,k) cin >> B[j].fs >> B[j].sc;
        A.pb(B);
    }
    cout << dp(0,0) << endl;
    return 0;
}


