#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
long long dp[3 * maxn];
int main() {
  memset(dp, 0, sizeof(dp));
  int n, cnt = 0;
  long long ans = 0;
  scanf("%d", &n);
  while (n--) {
    int k;
    scanf("%d", &k);
    int c1[3] = {-1, -1, -1}, c2 = -1, c3 = -1;
    while (k--) {
      int c, d;
      scanf("%d%d", &c, &d);
      if (c == 2 && d > c2) c2 = d;
      if (c == 3 && d > c3) c3 = d;
      if (c == 1) {
        if (d > c1[0]) {
          c1[2] = c1[1];
          c1[1] = c1[0];
          c1[0] = d;
        } else if (d > c1[1]) {
          c1[2] = c1[1];
          c1[1] = d;
        } else if (d > c1[2])
          c1[2] = d;
      }
    }
    int card1 = max(c1[0], max(c2, c3));
    int card2 = max(c1[0] + c1[1], c1[0] + c2);
    int card3 = c1[0] + c1[1] + c1[2];
    for (int i = cnt; i >= max(0, cnt - 200); i--) {
      if (c1[0] != -1 && c1[1] != -1 && c1[2] != -1)
        dp[i + 3] =
            max(dp[i + 3], dp[i] + (i % 10 >= 7 ? c1[0] * 2 + c1[1] + c1[2]
                                                : c1[0] + c1[1] + c1[2]));
      if (c1[0] != -1) {
        if (c1[1] != -1)
          dp[i + 2] = max(dp[i + 2], dp[i] + (i % 10 >= 8 ? c1[0] * 2 + c1[1]
                                                          : c1[0] + c1[1]));
        if (c2 != -1)
          dp[i + 2] =
              max(dp[i + 2],
                  dp[i] + (i % 10 >= 8 ? 2 * (max(c1[0], c2)) + min(c1[0], c2)
                                       : c1[0] + c2));
      }
      dp[i + 1] = max(dp[i + 1], dp[i] + (i % 10 == 9 ? card1 * 2 : card1));
    }
    if (dp[cnt + 3])
      cnt += 3;
    else if (dp[cnt + 2])
      cnt += 2;
    else if (dp[cnt + 1])
      cnt += 1;
  }
  for (int i = cnt; i >= max(0, cnt - 200); i--) ans = max(ans, dp[i]);
  printf("%lld", ans);
  return 0;
}
