#include <bits/stdc++.h>
using namespace std;
const long long LEN = (long long)2e5;
int n;
array<long long, 5> a[LEN];
array<long long, 10> dp[LEN];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> n;
  for (int i = 0; i < n; i++) {
    int k;
    cin >> k;
    while (k--) {
      long long c, d;
      cin >> c >> d;
      if (c == 3)
        a[i][4] = max(a[i][4], d);
      else if (c == 2)
        a[i][3] = max(a[i][3], d);
      else {
        a[i][0] = max(a[i][0], d);
        sort(a[i].begin(), a[i].begin() + 3);
      }
    }
  };
  ;
  dp[0][1] = max(max(a[0][2], a[0][3]), a[0][4]);
  dp[0][2] = a[0][2] + max(a[0][1], a[0][3]);
  if (dp[0][2] == a[0][2] || !a[0][2]) dp[0][2] = 0;
  dp[0][3] = (a[0][0] > 0) * (a[0][0] + a[0][1] + a[0][2]);
  for (int i = 1; i < n; i++) {
    long long z[4][2];
    for (int i = 0; i < 4; i++) z[i][0] = z[i][1] = 0;
    z[1][0] = max(max(a[i][2], a[i][3]), a[i][4]);
    z[2][0] = a[i][2] + max(a[i][1], a[i][3]);
    if (z[2][0] == a[i][2] || !a[i][2]) z[2][0] = 0;
    z[3][0] = (a[i][0] > 0) * (a[i][0] + a[i][1] + a[i][2]);
    z[1][1] = 2 * z[1][0];
    z[2][1] = (z[2][0] > 0) * (z[2][0] + max(a[i][2], max(a[i][1], a[i][3])));
    z[3][1] = (z[3][0] > 0) * (z[3][0] + a[i][2]);
    copy(dp[i - 1].begin(), dp[i - 1].end(), dp[i].begin());
    for (int j = 0; j <= 9; j++)
      for (int q = 1; q <= 3; q++) {
        int k = j - q, w = 0;
        if (k < 0) {
          k += 10;
          w = 1;
        }
        if (z[q][w] && dp[i - 1][k])
          dp[i][j] = max(dp[i][j], dp[i - 1][k] + z[q][w]);
      }
  }
  cout << *max_element(dp[n - 1].begin(), dp[n - 1].end());
  return 0;
}
