#include <bits/stdc++.h>
using namespace std;
const long long N = 3e5 + 15;
long long n;
long long dp[N][10];
vector<vector<long long>> a[N];
long long solve(long long i, long long j) {
  if (i == n) return 0;
  if (dp[i][j] != -1) return dp[i][j];
  long long res = 0;
  for (auto v : a[i]) {
    long long acum = j + v[0], gain = v[1];
    if (acum >= 10) {
      acum -= 10;
      gain += v[2];
    }
    res = max(res, gain + solve(i + 1, acum));
  }
  return dp[i][j] = res;
}
int main() {
  ios::sync_with_stdio(false);
  memset(dp, -1, sizeof(dp));
  cin >> n;
  for (long long i = 0; i < n; i++) {
    vector<long long> g[4];
    for (long long j = 1; j < 4; j++)
      g[j].push_back(-N), g[j].push_back(-N), g[j].push_back(-N);
    long long x;
    cin >> x;
    while (x--) {
      long long l, r;
      cin >> l >> r;
      g[l].push_back(r);
    }
    for (long long j = 1; j < 4; j++) sort(g[j].rbegin(), g[j].rend());
    long long c1 = max({g[1][0], g[2][0], g[3][0]});
    if (c1 > 0) a[i].push_back({1, c1, c1});
    pair<long long, long long> c2 =
        max(make_pair(g[1][0] + g[1][1], g[1][0]),
            make_pair(g[2][0] + g[1][0], max(g[2][0], g[1][0])));
    if (c2.first > 0) a[i].push_back({2, c2.first, c2.second});
    long long c3 = g[1][0] + g[1][1] + g[1][2];
    if (c3 > 0) a[i].push_back({3, c3, g[1][0]});
  }
  cout << solve(0, 0) << endl;
}
