#include <bits/stdc++.h>
using namespace std;
const int N = 200005;
const int INF = 2e9;
int n, k;
struct Node {
  int c, d;
} t[N];
priority_queue<int> max1;
vector<Node> a[N];
int dp[N][11];
int main() {
  scanf("%d", &n);
  for (int j = 1; j <= n; j++) {
    scanf("%d", &k);
    int max3 = -INF, max2 = -INF;
    while (!max1.empty()) max1.pop();
    for (int i = 1; i <= k; i++) {
      scanf("%d%d", &t[i].c, &t[i].d);
      if (t[i].c == 2)
        max2 = max(max2, t[i].d);
      else if (t[i].c == 3)
        max3 = max(max3, t[i].d);
      else if (t[i].c == 1)
        max1.push(t[i].d);
    }
    if (max2 != -INF) a[j].push_back((Node){2, max2});
    if (max3 != -INF) a[j].push_back((Node){3, max3});
    int cnt = 0;
    while (!max1.empty() && cnt < 3) {
      a[j].push_back((Node){1, max1.top()});
      max1.pop();
      cnt++;
    }
  }
  for (int i = 0; i <= n; i++)
    for (int j = 0; j < 10; j++) dp[i][j] = -INF;
  for (int i = 1; i <= n; i++) {
    int cnt[4] = {0};
    int val11 = 0, val12 = 0, val13 = 0, val2 = 0, val3 = 0;
    int max1 = 0, sec1 = 0;
    for (int j = 0; j < a[i].size(); j++) {
      cnt[a[i][j].c]++;
      if (a[i][j].c == 1) {
        if (!val11)
          val11 = a[i][j].d;
        else if (!val12)
          val12 = a[i][j].d;
        else if (!val13)
          val13 = a[i][j].d;
      } else if (a[i][j].c == 2)
        val2 = a[i][j].d;
      else if (a[i][j].c == 3)
        val3 = a[i][j].d;
    }
    max1 = max(val11, max(val12, val13));
    if (max1 == val11)
      sec1 = max(val12, val13);
    else if (max1 == val12)
      sec1 = max(val11, val13);
    else if (max1 == val13)
      sec1 = max(val11, val12);
    if (i == 1) {
      dp[i][1] = max(max1, max(val2, val3));
      dp[i][2] = max1 + sec1;
      dp[i][3] = val11 + val12 + val13;
      continue;
    }
    if (cnt[1] >= 1) {
      for (int j = 0; j < 9; j++)
        dp[i][j + 1] = max(dp[i][j + 1], dp[i - 1][j] + max1);
      dp[i][0] = max(dp[i][0], dp[i - 1][9] + max1 * 2);
    }
    if (cnt[1] >= 2) {
      for (int j = 0; j < 8; j++)
        dp[i][j + 2] = max(dp[i][j + 2], dp[i - 1][j] + max1 + sec1);
      dp[i][0] = max(dp[i][0], dp[i - 1][8] + max1 * 2 + sec1);
      dp[i][1] = max(dp[i][1], dp[i - 1][9] + max1 * 2 + sec1);
    }
    if (cnt[1] >= 3) {
      for (int j = 0; j < 7; j++)
        dp[i][j + 3] = max(dp[i][j + 3], dp[i - 1][j] + val11 + val12 + val13);
      dp[i][0] = max(dp[i][0], dp[i - 1][7] + val11 + val12 + val13 + max1);
      dp[i][1] = max(dp[i][1], dp[i - 1][8] + val11 + val12 + val13 + max1);
      dp[i][2] = max(dp[i][2], dp[i - 1][9] + val11 + val12 + val13 + max1);
    }
    if (cnt[2] && cnt[1]) {
      for (int j = 0; j < 8; j++)
        dp[i][j + 2] = max(dp[i][j + 2], dp[i - 1][j] + max1 + val2);
      dp[i][0] = max(dp[i][0], dp[i - 1][8] + max1 + val2 + max(max1, val12));
      dp[i][1] = max(dp[i][1], dp[i - 1][9] + max1 + val2 + max(max1, val12));
    }
    if (cnt[2] && !cnt[1]) {
      for (int j = 0; j < 9; j++)
        dp[i][j + 1] = max(dp[i][j + 1], dp[i - 1][j] + val2);
      dp[i][0] = max(dp[i][0], dp[i - 1][9] + val2 * 2);
    }
    if (cnt[3]) {
      for (int j = 0; j < 9; j++)
        dp[i][j + 1] = max(dp[i][j + 1], dp[i - 1][j] + val3);
      dp[i][0] = max(dp[i][0], dp[i - 1][9] + val3 * 2);
    }
  }
  int ans = 0;
  for (int j = 0; j < 10; j++) ans = max(ans, dp[n][j]);
  printf("%d\n", ans);
  return 0;
}
