#include <bits/stdc++.h>
using namespace std;
bool isrange(int second, int first, int n, int m) {
  if (0 <= second && second < n && 0 <= first && first < m) return true;
  return false;
}
int dy[4] = {1, 0, -1, 0}, dx[4] = {0, 1, 0, -1},
    ddy[8] = {1, 0, -1, 0, 1, 1, -1, -1}, ddx[8] = {0, 1, 0, -1, 1, -1, 1, -1};
bool tmr(long long int a, long long int b) { return a > b; }
const int MAX = 202020;
long long int dp[MAX][10], tmp[10][4], tmp2[10][4];
int lim = 0;
int main(void) {
  int n;
  scanf("%d", &n);
  memset(dp, -1, sizeof(dp));
  dp[0][0] = 0;
  for (int e = 1; e <= n; e++) {
    int k;
    scanf("%d", &k);
    memset(tmp, -1, sizeof(tmp));
    for (int p = 0; p < k; p++) {
      for (int r = 0; r < 10; r++)
        for (int q = 0; q < 4; q++) tmp2[r][q] = tmp[r][q];
      int c, d;
      scanf("%d%d", &c, &d);
      for (int r = 0; r < 10; r++) {
        if (r < 9) {
          if (dp[e - 1][r] != -1) {
            tmp2[r + 1][c] = max(tmp2[r + 1][c], dp[e - 1][r] + d);
          }
        } else {
          if (dp[e - 1][r] != -1) {
            tmp2[0][c] = max(tmp2[0][c], dp[e - 1][r] + 2 * d);
          }
        }
      }
      for (int r = 0; r < 10; r++) {
        for (int p = 0; p <= 2; p++) {
          if (p + c > 3) continue;
          if (r < 9) {
            if (tmp[r][p] != -1) {
              tmp2[r + 1][p + c] = max(tmp2[r + 1][p + c], tmp[r][p] + d);
            }
          } else {
            if (tmp[r][p] != -1) {
              tmp2[0][p + c] = max(tmp2[0][p + c], tmp[r][p] + 2 * d);
            }
          }
        }
      }
      for (int r = 0; r < 10; r++) {
        for (int p = 0; p < 4; p++) tmp[r][p] = tmp2[r][p];
      }
    }
    for (int r = 0; r < 10; r++) {
      for (int p = 0; p < 4; p++) {
        dp[e][r] = max(dp[e - 1][r], tmp[r][p]);
      }
    }
  }
  long long int ans = -1;
  for (int e = 0; e < 10; e++) ans = max(ans, dp[n][e]);
  printf("%lld\n", ans);
  return 0;
}
