#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
long long dp[maxn][10];
int c1[maxn], c2, c3;
long long g[4][2];
bool cmp(int a, int b) { return a > b; }
int main() {
  int n;
  scanf("%d", &n);
  int k;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &k);
    int cnt = 0, c, v;
    c2 = 0, c3 = 0;
    for (int ii = 0; ii < 4; ii++) {
      for (int j = 0; j < 2; j++) {
        g[ii][j] = 0;
      }
    }
    for (int j = 0; j < k; j++) {
      scanf("%d%d", &c, &v);
      if (c == 1) {
        c1[cnt++] = v;
      } else if (c == 2) {
        c2 = max(c2, v);
      } else {
        c3 = max(c3, v);
      }
    }
    sort(c1, c1 + cnt, cmp);
    long long mmax = 0;
    if (cnt != 0) {
      mmax = max(max(c1[0], c2), c3);
    } else {
      mmax = max(c2, c3);
    }
    g[1][0] = mmax, g[1][1] = mmax << 1;
    if (cnt >= 2) {
      g[2][0] = c1[0] + max(c1[1], c2);
      g[2][1] = g[2][0] + max(c1[0], c2);
    } else if (cnt == 1 && c2 > 0) {
      g[2][0] = c1[0] + c2;
      g[2][1] = g[2][0] + max(c1[0], c2);
    }
    if (cnt >= 3) {
      g[3][0] = c1[0] + c1[1] + c1[2];
      g[3][1] = g[3][0] + c1[0];
    }
    for (int ii = 0; ii < 10; ii++) {
      if (dp[i - 1][ii] == 0 && ii != 0) continue;
      for (int j = 0; j < 4; j++) {
        if (g[j][0] == 0 && j != 0) continue;
        if (ii + j >= 10) {
          dp[i][(ii + j) % 10] =
              max(max(dp[i - 1][(ii + j) % 10], dp[i - 1][ii] + g[j][1]),
                  dp[i][(ii + j) % 10]);
        } else {
          dp[i][(ii + j) % 10] =
              max(max(dp[i - 1][(ii + j) % 10], dp[i - 1][ii] + g[j][0]),
                  dp[i][(ii + j) % 10]);
        }
      }
    }
  }
  long long ans = 0;
  for (int i = 0; i < 10; i++) {
    ans = max(ans, dp[n][i]);
  }
  printf("%I64d\n", ans);
}
