#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
const int maxn2 = 3e6 + 5;
const long long mod = 1e9 + 7;
const long long inf = 1e18;
long long dp[11][maxn], maxx[3][3];
vector<long long> g[3][maxn];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    int k;
    scanf("%d", &k);
    memset(maxx, 0, sizeof(maxx));
    for (int j = 1; j <= k; j++) {
      int x;
      long long y;
      scanf("%d%lld", &x, &y);
      if (x == 1) {
        if (maxx[0][0] <= y) {
          maxx[2][0] = maxx[1][0];
          maxx[1][0] = maxx[0][0];
          maxx[0][0] = y;
        } else if (maxx[1][0] <= y) {
          maxx[2][0] = maxx[1][0];
          maxx[1][0] = y;
        } else if (maxx[2][0] <= y) {
          maxx[2][0] = y;
        }
      } else if (x == 2) {
        if (maxx[0][1] <= y) {
          maxx[2][1] = maxx[1][1];
          maxx[1][1] = maxx[0][1];
          maxx[0][1] = y;
        } else if (maxx[1][1] <= y) {
          maxx[2][1] = maxx[1][1];
          maxx[1][1] = y;
        } else if (maxx[2][1] <= y) {
          maxx[2][1] = y;
        }
      } else {
        if (maxx[0][2] <= y) {
          maxx[2][2] = maxx[1][2];
          maxx[1][2] = maxx[0][2];
          maxx[0][2] = y;
        } else if (maxx[1][2] <= y) {
          maxx[2][2] = maxx[1][2];
          maxx[1][2] = y;
        } else if (maxx[2][2] <= y) {
          maxx[2][2] = y;
        }
      }
    }
    for (int j = 0; j < 3; j++) {
      for (int w = 0; w < 3; w++) {
        if (maxx[w][j]) g[j][i].push_back(maxx[w][j]);
      }
    }
  }
  memset(dp, -1, sizeof(dp));
  dp[0][0] = 0;
  long long ac = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 9; j >= 0; j--) {
      dp[j][i] = dp[j][i - 1];
      if (g[0][i].size() >= 3) {
        if (j == 0 && dp[(j - 3 + 10) % 10][i - 1] != -1)
          dp[j][i] =
              max(dp[j][i], dp[(j - 3 + 10) % 10][i - 1] + g[0][i][0] * 2 +
                                g[0][i][1] + g[0][i][2]);
        else if (dp[(j - 3 + 10) % 10][i - 1] != -1)
          dp[j][i] = max(dp[j][i], dp[(j - 3 + 10) % 10][i - 1] + g[0][i][0] +
                                       g[0][i][1] + g[0][i][2]);
      }
      if (g[0][i].size() >= 1 && g[1][i].size() >= 1) {
        if (j == 0 && dp[(j - 2 + 10) % 10][i - 1] != -1)
          dp[j][i] =
              max(dp[j][i], dp[(j - 2 + 10) % 10][i - 1] + g[0][i][0] +
                                g[1][i][0] + max(g[0][i][0], g[1][i][0]));
        else if (dp[(j - 2 + 10) % 10][i - 1] != -1)
          dp[j][i] = max(
              dp[j][i], dp[(j - 2 + 10) % 10][i - 1] + g[0][i][0] + g[1][i][0]);
      }
      long long w = 0;
      if (g[0][i].size()) w = max(w, g[0][i][0]);
      if (g[1][i].size()) w = max(w, g[1][i][0]);
      if (g[2][i].size()) w = max(w, g[2][i][0]);
      if (j == 0 && dp[(j - 1 + 10) % 10][i - 1] != -1 && w)
        dp[j][i] = max(dp[j][i], dp[(j - 1 + 10) % 10][i - 1] + w * 2);
      else if (dp[(j - 1 + 10) % 10][i - 1] != -1 && w)
        dp[j][i] = max(dp[j][i], dp[(j - 1 + 10) % 10][i - 1] + w);
      if (i == n) ac = max(ac, dp[j][i]);
    }
  }
  printf("%lld\n", ac);
  return 0;
}
