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