#include <bits/stdc++.h>
using ll = long long;
using ld = long double;
using ull = unsigned long long;
using namespace std;
const int N = 2e5 + 1;
int n, mx3[N], mx2[N], mx1[3][N];
ll dp[10][N];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> n;
  for (int i = (1); i <= (n); ++i) {
    int k;
    cin >> k;
    for (int j = (1); j <= (k); ++j) {
      int c, d;
      cin >> c >> d;
      if (c == 1) {
        if (d > mx1[0][i])
          mx1[2][i] = mx1[1][i], mx1[1][i] = mx1[0][i], mx1[0][i] = d;
        else if (d > mx1[1][i])
          mx1[2][i] = mx1[1][i], mx1[1][i] = d;
        else if (d > mx1[2][i])
          mx1[2][i] = d;
      } else if (c == 2)
        mx2[i] = max(mx2[i], d);
      else
        mx3[i] = max(mx3[i], d);
    }
  }
  fill(&dp[0][0], &dp[0][0] + 10 * N, -1e9);
  dp[0][0] = 0;
  for (int i = (1); i <= (n); ++i)
    for (int j = (0); j <= (9); ++j) {
      int mul = (j == 0 ? 2 : 1);
      dp[j][i] = dp[j][i - 1];
      if (dp[(j + 9) % 10][i - 1] != -1e9)
        dp[j][i] = max(dp[j][i], dp[(j + 9) % 10][i - 1] + mx1[0][i] * mul);
      if (dp[(j + 8) % 10][i - 1] != -1e9)
        dp[j][i] = max(dp[j][i],
                       dp[(j + 8) % 10][i - 1] + mx1[1][i] + mx1[0][i] * mul);
      if (dp[(j + 7) % 10][i - 1] != -1e9)
        dp[j][i] = max(dp[j][i], dp[(j + 7) % 10][i - 1] + mx1[2][i] +
                                     mx1[1][i] + mx1[0][i] * mul);
      if (dp[(j + 9) % 10][i - 1] != -1e9)
        dp[j][i] = max(dp[j][i], dp[(j + 9) % 10][i - 1] + mx2[i] * mul);
      if (dp[(j + 8) % 10][i - 1] != -1e9)
        dp[j][i] =
            max(dp[j][i], dp[(j + 8) % 10][i - 1] + min(mx2[i], mx1[0][i]) +
                              max(mx2[i], mx1[0][i]) * mul);
      if (dp[(j + 9) % 10][i - 1] != -1e9)
        dp[j][i] = max(dp[j][i], dp[(j + 9) % 10][i - 1] + mx3[i] * mul);
    }
  ll ans = 0;
  for (int i = (0); i <= (9); ++i) ans = max(ans, dp[i][n]);
  cout << ans;
  return 0;
}
