#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
const int M = 1e9 + 7;
const int inf = 1e9 + 7;
const long long INF = 1e18;
int n, k, c, d;
long long f[2][10];
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n;
  int s = 0;
  for (int j = 0; j < 10; j++) f[s][j] = -inf, f[s ^ 1][j] = -inf;
  f[s][0] = 0;
  f[s ^ 1][0] = 0;
  for (int i = 0; i < n; i++) {
    cin >> k;
    vector<int> t1(3, 0);
    int t2 = 0, t3 = 0;
    for (int j = 0; j < k; j++) {
      cin >> c >> d;
      if (c == 1) {
        if (d > t1[0]) {
          t1[2] = t1[1];
          t1[1] = t1[0];
          t1[0] = d;
        } else if (d > t1[1]) {
          t1[2] = t1[1];
          t1[1] = d;
        } else if (d > t1[2]) {
          t1[2] = d;
        }
      }
      if (c == 2) {
        t2 = max(t2, d);
      }
      if (c == 3) {
        t3 = max(t3, d);
      }
    }
    for (int j = 0; j <= 9; j++) {
      for (int l = 1; l <= 3; l++) {
        if (l == 1) {
          int p = max(t3, t2);
          p = max(p, t1[0]);
          if (j == 0) {
            f[s ^ 1][j] = max(f[s][9] + p + p, f[s ^ 1][j]);
          } else {
            f[s ^ 1][j] = max(f[s][j - 1] + p, f[s ^ 1][j]);
          }
        } else if (l == 2) {
          int p1, p2;
          if (t2 > t1[1]) {
            p1 = max(t2, t1[0]);
            p2 = min(t2, t1[0]);
          } else {
            p1 = t1[0], p2 = t1[1];
          }
          if (j == 0) {
            f[s ^ 1][j] = max(f[s][8] + p2 + p1 + p1, f[s ^ 1][j]);
          } else {
            f[s ^ 1][j] = max(f[s][(j - 2 + 10) % 10] + p1 + p2, f[s ^ 1][j]);
          }
        } else {
          int p = t1[0] + t1[1] + t1[2];
          if (j == 0) {
            f[s ^ 1][j] = max(f[s][7] + p + t1[0], f[s ^ 1][j]);
          } else {
            f[s ^ 1][j] = max(f[s][(j - 3 + 10) % 10] + p, f[s ^ 1][j]);
          }
        }
      }
    }
    s ^= 1;
  }
  long long ans = 0;
  for (int j = 0; j < 10; j++) {
    ans = max(ans, f[s][j]);
  }
  printf("%lld\n", ans);
}
