#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, K;
  cin >> N;
  long long mod[10] = {0};
  for (int i = 0; i < N; i++) {
    vector<long long> A[3];
    cin >> K;
    long long c, v;
    for (int j = 0; j < K; j++) {
      cin >> c >> v;
      if (c <= 3) A[c - 1].push_back(v);
    }
    for (int j = 0; j < 3; j++) sort(A[j].rbegin(), A[j].rend());
    pair<long long, long long> x[3];
    x[0] = x[1] = x[2] = make_pair(0, 0);
    if (A[0].size() > 0) x[0] = make_pair(A[0][0], A[0][0]);
    if (A[1].size() > 0 && A[1][0] > x[0].first)
      x[0] = make_pair(A[1][0], A[1][0]);
    if (A[2].size() > 0 && A[2][0] > x[0].first)
      x[0] = make_pair(A[2][0], A[2][0]);
    if (A[0].size() > 1)
      x[1] = make_pair(A[0][0] + A[0][1], max(A[0][0], A[0][1]));
    if (A[0].size() > 0 && A[1].size() > 0 && (A[0][0] + A[1][0]) > x[1].first)
      x[1] = make_pair(A[0][0] + A[1][0], max(A[0][0], A[1][0]));
    if (A[0].size() > 2)
      x[2] = make_pair(A[0][0] + A[0][1] + A[0][2],
                       max(A[0][0], max(A[0][1], A[0][2])));
    if (i == 0) {
      for (int j = 0; j < 3; j++) mod[j + 1] = x[j].first;
      continue;
    }
    long long temp[10] = {0};
    for (int k = 1; k <= 3; k++) temp[k] = max(x[k - 1].first, mod[k]);
    for (int j = 0; j < 10; j++)
      if (mod[j] != 0)
        for (int k = 0; k < 3; k++) {
          if (x[k].first == 0) continue;
          if ((j + k + 1) % 10 < j)
            temp[(j + k + 1) % 10] =
                max(temp[(j + k + 1) % 10], mod[j] + x[k].first + x[k].second);
          else
            temp[(j + k + 1) % 10] =
                max(temp[(j + k + 1) % 10], mod[j] + x[k].first);
        }
    for (int j = 0; j < 10; j++) mod[j] = temp[j] == 0 ? mod[j] : temp[j];
  }
  long long ans = 0;
  for (int i = 0; i < 10; i++) ans = max(ans, mod[i]);
  cout << ans << endl;
}
