#include <bits/stdc++.h>
using namespace std;
const int N = 200010;
const int M = 1e9 + 7;
template <class T>
inline T read(T &x) {
  int f = 1;
  char ch = getchar();
  x = 0;
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x *= f;
}
int n, num1, num2, num3;
long long f[N][10];
struct Card {
  int c, d;
  Card(int c = 0, int d = 0) : c(c), d(d) {}
  int operator<(const Card other) const { return d > other.d; }
} a[N], c1[4], c2, c3;
int main() {
  read(n);
  memset(f[0], 128, sizeof(f[0]));
  f[0][0] = 0;
  for (int(i) = (1); (i) <= (n); (i)++) {
    int x;
    read(x);
    for (int(j) = (1); (j) <= (x); (j)++) read(a[j].c), read(a[j].d);
    sort(a + 1, a + x + 1);
    num1 = num2 = num3 = 0;
    for (int(j) = (1); (j) <= (x); (j)++) {
      if (num1 < 3 && a[j].c == 1) c1[++num1] = a[j];
      if (num2 < 1 && a[j].c == 2) c2 = a[j], num2 = 1;
      if (num3 < 1 && a[j].c == 3) c3 = a[j], num3 = 1;
    }
    for (int(j) = (0); (j) <= (9); (j)++) {
      int _j, ex;
      if (num1 == 3) {
        ex = (j + 3 > 9) ? c1[1].d : 0;
        _j = (j + 3) % 10;
        f[i][_j] =
            max(f[i][_j], f[i - 1][j] + c1[1].d + c1[2].d + c1[3].d + ex);
      }
      if (num1 >= 2) {
        ex = (j + 2 > 9) ? c1[1].d : 0;
        _j = (j + 2) % 10;
        f[i][_j] = max(f[i][_j], f[i - 1][j] + c1[1].d + c1[2].d + ex);
      }
      if (num1 >= 1 && num2 == 1) {
        ex = (j + 2 > 9) ? max(c1[1].d, c2.d) : 0;
        _j = (j + 2) % 10;
        f[i][_j] = max(f[i][_j], f[i - 1][j] + c1[1].d + c2.d + ex);
      }
      if (num1 >= 1) {
        ex = (j + 1 > 9) ? c1[1].d : 0;
        _j = (j + 1) % 10;
        f[i][_j] = max(f[i][_j], f[i - 1][j] + c1[1].d + ex);
      }
      if (num2 == 1) {
        ex = (j + 1 > 9) ? c2.d : 0;
        _j = (j + 1) % 10;
        f[i][_j] = max(f[i][_j], f[i - 1][j] + c2.d + ex);
      }
      if (num3 == 1) {
        ex = (j + 1 > 9) ? c3.d : 0;
        _j = (j + 1) % 10;
        f[i][_j] = max(f[i][_j], f[i - 1][j] + c3.d + ex);
      }
      f[i][j] = max(f[i][j], f[i - 1][j]);
    }
    for (int(j) = (0); (j) <= (9); (j)++)
      if (f[i][j] == 0) f[i][j] = f[0][1];
  }
  long long ans = 0;
  for (int(j) = (0); (j) <= (9); (j)++) ans = max(ans, f[n][j]);
  printf("%lld\n", ans);
  return 0;
}
