#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
const int INF = 1e18 + 1;
const int mod = 1e9 + 7;
void docfile() {
  if (ifstream("FORM.inp")) {
    freopen("FORM.inp", "r", stdin);
    freopen("FORM.out", "w", stdout);
  } else if (ifstream("MESSAGES"
                      ".inp")) {
    freopen(
        "MESSAGES"
        ".inp",
        "r", stdin);
    freopen(
        "MESSAGES"
        ".out",
        "w", stdout);
  }
}
const int N = 2e5 + 5;
int n, dp[N][5][5], nc[N][5][5];
void input() { cin >> n; }
void solve() {
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j <= 3; j++)
      for (int k = 0; k <= 3; k++)
        if (dp[i][0][0] < dp[i - 1][j][k]) {
          dp[i][0][0] = dp[i - 1][j][k];
          nc[i][0][0] = nc[i - 1][j][k];
        }
    int m, c, d;
    cin >> m;
    while (m--) {
      cin >> c >> d;
      for (int j = 3; j >= 0; j--)
        for (int k = 3; k >= 0; k--) {
          int tmp = d;
          if (nc[i][j - c][k - 1] == 9) tmp += d;
          if (j >= c && k >= 1 && dp[i][j][k] < dp[i][j - c][k - 1] + tmp) {
            dp[i][j][k] = dp[i][j - c][k - 1] + tmp;
            nc[i][j][k] = nc[i][j - c][k - 1] + 1;
          }
        }
      for (int j2 = 0; j2 <= 3; j2++)
        for (int k2 = 0; k2 <= 3; k2++) {
          int tmp = d;
          if (nc[i - 1][j2][k2] == 9) tmp += d;
          if (dp[i][c][1] < dp[i - 1][j2][k2] + tmp) {
            dp[i][c][1] = dp[i - 1][j2][k2] + tmp;
            nc[i][c][1] = nc[i - 1][j2][k2] + 1;
          }
        }
    }
  }
  int ans = 0;
  for (int j = 0; j <= 3; j++)
    for (int k = 0; k <= 3; k++) ans = max(ans, dp[n][j][k]);
  cout << ans;
}
int main() {
  docfile();
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  input();
  solve();
}
