#include <bits/stdc++.h>
using namespace std;
const long long N = 1e5 + 9;
long long dp[2 * N][5][12], lim;
struct tree {
  long long c, d;
  bool f;
} ar[N * 2];
long long maximize(long long id, long long cs, long long nm) {
  if (id > lim) return 0;
  if (~dp[id][cs][nm]) return dp[id][cs][nm];
  long long ret = maximize(id + 1, (ar[id].f ? 0 : cs), nm);
  if (cs + ar[id].c <= 3)
    ret = max(ret, (nm ? 1 : 2) * (ar[id].d) +
                       maximize(id + 1, (ar[id].f ? 0 : (cs + ar[id].c)),
                                (nm + 1) % 10));
  return dp[id][cs][nm] = ret;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long n;
  cin >> n;
  while (n--) {
    long long k;
    cin >> k;
    while (k--) {
      ++lim;
      cin >> ar[lim].c >> ar[lim].d;
    }
    ar[lim].f = true;
  }
  memset(dp, -1, sizeof dp);
  cout << maximize(1, 0, 1) << "\n";
}
