#include <bits/stdc++.h>
using namespace std;
const long long N = 2e5 + 5;
const long long oo = 1e14;
long long n, k, rs, f[4][4][2], F[N][4][2], dp[N][15];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (long long i = 1; i <= n; i++) {
    cin >> k;
    for (long long cnt = 1; cnt <= 3; cnt++)
      for (long long z = 0; z <= 3; z++) f[cnt][z][0] = f[cnt][z][1] = -oo;
    for (long long j = 1; j <= k; j++) {
      long long c, d;
      cin >> c >> d;
      for (long long cnt = min(3ll, k); cnt >= 1; cnt--)
        for (long long z = 3; z >= 1; z--)
          if (z >= c && z >= cnt) {
            f[cnt][z][1] = max(max(f[cnt][z][1], f[cnt - 1][z - c][1] + d),
                               f[cnt - 1][z - c][0] + 2 * d);
            f[cnt][z][0] = max(f[cnt][z][0], f[cnt - 1][z - c][0] + d);
          }
    }
    for (long long bit = 0; bit <= 1; bit++)
      for (long long z = 1; z <= 3; z++)
        for (long long cnt = 1; cnt <= 3; cnt++)
          F[i][cnt][bit] = max(F[i][cnt][bit], f[cnt][z][bit]);
  }
  for (long long cnt = 1; cnt <= 10; cnt++) dp[0][cnt] = -oo;
  dp[0][10] = 0;
  for (long long i = 1; i <= n; i++)
    for (long long cnt = 1; cnt <= 10; cnt++) {
      dp[i][cnt] = dp[i - 1][cnt];
      for (long long j = 1; j <= 3; j++) {
        long long tt = dp[i - 1][(cnt - j + 9) % 10 + 1];
        long l = (cnt - j + 9) % 10 + 1, r = l + j;
        if (l < 10 && 10 <= r)
          tt += F[i][j][1];
        else
          tt += F[i][j][0];
        dp[i][cnt] = max(dp[i][cnt], tt);
      }
      rs = max(rs, dp[i][cnt]);
    }
  cout << rs << '\n';
}
