#include <bits/stdc++.h>
using namespace std;
long long f[200005][15];
long long i, j, k, m, n, o, p, l, s, t, times, max1, max2, max3, max4, max5;
void read(long long &x) {
  char ch = getchar();
  x = 0;
  while (ch < '0' || ch > '9') ch = getchar();
  while (ch >= '0' && ch <= '9')
    x = (x << 3) + (x << 1) + ch - 48, ch = getchar();
}
int main() {
  read(n);
  memset(f, -60, sizeof(f));
  f[0][0] = 0;
  for (i = 1; i <= n; i++) {
    read(m);
    max1 = max2 = max3 = max4 = max5 = 0;
    for (j = 1; j <= m; j++) {
      read(o);
      read(p);
      if (o == 1) {
        if (p >= max1)
          max3 = max2, max2 = max1, max1 = p;
        else if (p >= max2)
          max3 = max2, max2 = p;
        else
          max3 = max(max3, p);
      } else if (o == 2)
        max4 = max(max4, p);
      else
        max5 = max(max5, p);
    }
    for (j = 1; j <= 3; j++) {
      for (k = 0; k <= 9; k++) {
        if (j == 1) {
          if (f[i - 1][k] >= 0) {
            if (max1)
              f[i][(k + 1) % 10] =
                  max(f[i][(k + 1) % 10], f[i - 1][k] + max1 * (1 + (k == 9)));
          }
        } else if (j == 2) {
          if (f[i - 1][k] >= 0) {
            if (max1 && max2)
              f[i][(k + 2) % 10] =
                  max(f[i][(k + 2) % 10],
                      f[i - 1][k] + max1 * (1 + (k >= 8)) + max2);
            if (max4)
              f[i][(k + 1) % 10] =
                  max(f[i][(k + 1) % 10], f[i - 1][k] + max4 * (1 + (k >= 9)));
          }
        } else if (j == 3) {
          if (f[i - 1][k] >= 0) {
            if (max1 && max2 && max3)
              f[i][(k + 3) % 10] =
                  max(f[i][(k + 3) % 10],
                      f[i - 1][k] + max1 * (1 + (k >= 7)) + max2 + max3);
            if (max1 && max4)
              f[i][(k + 2) % 10] =
                  max(f[i][(k + 2) % 10], f[i - 1][k] +
                                              max(max1, max4) * (1 + (k >= 8)) +
                                              min(max1, max4));
            if (max5)
              f[i][(k + 1) % 10] =
                  max(f[i][(k + 1) % 10], f[i - 1][k] + max5 * (1 + (k >= 9)));
          }
        }
      }
    }
  }
  s = 0;
  for (i = 0; i <= 9; i++) s = max(s, f[n][i]);
  printf("%lld\n", s);
  return 0;
}
