#include <bits/stdc++.h>
using namespace std;
long long c[600005], d[600005], e[600005] = {};
long long dp[600005][4][10];
int n;
int idx = 0;
long long rec(int x, int y, int z) {
  if (z > 9 || y < 0) cout << "a";
  if (x == idx) return 0;
  if (dp[x][y][z] != -1) return dp[x][y][z];
  long long ret = 0;
  int yy = y;
  if (e[x] == 1) y = 3;
  if (c[x] <= y) {
    if (z < 9)
      ret = max(ret, rec(x + 1, y - c[x], z + 1) + d[x]);
    else
      ret = max(ret, rec(x + 1, y - c[x], 0) + d[x] * 2);
  }
  ret = max(ret, rec(x + 1, y, z));
  return dp[x][yy][z] = ret;
}
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int m;
    cin >> m;
    e[idx] = 1;
    for (int j = 0; j < m; j++) {
      scanf("%i64d %i64d", &c[idx], &d[idx]);
      idx++;
    }
  }
  for (int i = 0; i <= 600000; i++)
    for (int j = 0; j <= 3; j++)
      for (int k = 0; k < 10; k++) dp[i][j][k] = -1;
  cout << rec(0, 0, 0) << endl;
}
