#include <bits/stdc++.h>
using namespace std;
int read() {
  int x = 0, f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = x * 10 + c - '0';
    c = getchar();
  }
  return x * f;
}
const double eps = 1e-9;
const int maxn = 2e5 + 10;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
int N, M, K;
long long dp[maxn][4][11];
int main() {
  scanf("%d", &N);
  memset(dp, -0x3f, sizeof(dp));
  dp[0][0][0] = 0;
  long long ans = 0;
  for (int i = 1; i <= N; i++) {
    int k;
    scanf("%d", &k);
    for (int j = 1; j <= k; j++) {
      int c;
      long long d;
      scanf("%d%lld", &c, &d);
      for (int p = 3 - c; p >= 0; p--) {
        for (int q = 8; q >= 0; q--) {
          long long val = d;
          dp[i][p + c][q + 1] = max(dp[i][p + c][q + 1], dp[i][p][q] + val);
          if (i == N) ans = max(ans, dp[i][p + c][(q + 1) % 10]);
        }
        int q = 9;
        long long val = 2 * d;
        dp[i][p + c][(q + 1) % 10] =
            max(dp[i][p + c][(q + 1) % 10], dp[i][p][q] + val);
        if (i == N) ans = max(ans, dp[i][p + c][(q + 1) % 10]);
      }
      for (int p = 0; p <= 3; p++) {
        for (int q = 0; q <= 9; q++) {
          long long val = (q == 9 ? d * 2 : d);
          dp[i][c][(q + 1) % 10] =
              max(dp[i][c][(q + 1) % 10], dp[i - 1][p][q] + val);
          if (i == N) ans = max(ans, dp[i][c][(q + 1) % 10]);
        }
      }
    }
  }
  printf("%lld\n", ans);
  ;
  return 0;
}
