#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  scanf("%d", &n);
  long long int dp[10][200020];
  for (int i = 0; i < 10; i++)
    for (int j = 0; j <= n; j++) dp[i][j] = -1e18;
  dp[0][0] = 0;
  for (int i = 0; i < n; i++) {
    int k;
    scanf("%d", &k);
    vector<long long int> vd[4];
    for (int j = 1; j <= 3; j++) vd[j].push_back(-1e18);
    vd[1].push_back(-1e18);
    vd[1].push_back(-1e18);
    for (int j = 0; j < k; j++) {
      int c;
      long long int d;
      scanf("%d %lld", &c, &d);
      vd[c].push_back(d);
    }
    for (int j = 1; j <= 3; j++)
      sort(vd[j].begin(), vd[j].end(), greater<long long int>());
    for (int j = 0; j < 10; j++) {
      long long int mx1 = max({vd[1][0], vd[2][0], vd[3][0]});
      if (j == 9)
        dp[0][i + 1] = max(dp[0][i + 1], dp[j][i] + 2 * mx1);
      else
        dp[j + 1][i + 1] = max(dp[j + 1][i + 1], dp[j][i] + mx1);
      long long int mx2;
      if (j == 8 || j == 9) {
        mx2 = max({vd[1][0] * 2 + vd[1][1], vd[1][0] * 2 + vd[2][0],
                   vd[1][0] + vd[2][0] * 2});
      } else {
        mx2 = max(vd[1][0] + vd[1][1], vd[1][0] + vd[2][0]);
      }
      dp[(j + 2) % 10][i + 1] = max(dp[(j + 2) % 10][i + 1], dp[j][i] + mx2);
      long long int mx3;
      if (j >= 7) {
        mx3 = 2 * vd[1][0] + vd[1][1] + vd[1][2];
      } else {
        mx3 = vd[1][0] + vd[1][1] + vd[1][2];
      }
      dp[(j + 3) % 10][i + 1] = max(dp[(j + 3) % 10][i + 1], dp[j][i] + mx3);
    }
  }
  long long int ans = -1e18;
  for (int i = 0; i < 10; i++) {
    ans = max(ans, dp[i][n]);
  }
  cout << ans << endl;
  return 0;
}
