#include <bits/stdc++.h>
using namespace std;
long long n, i, j, k, te;
long long a[202020];
long long b[202020];
long long c[202020];
long long d[200002][4][10];
long long depe(long long aa, long long bb, long long cc) {
  if (bb < 0) return -1e18;
  if (aa == n + 1) return 0;
  long long ret = d[aa][bb][cc];
  if (ret != -1) return d[aa][bb][cc];
  if (c[aa] != c[aa - 1]) bb = 3;
  ret = depe(aa + 1, bb, cc);
  if (cc == 9)
    ret = max(ret, depe(aa + 1, bb - a[aa], 0) + b[aa] * 2);
  else
    ret = max(ret, depe(aa + 1, bb - a[aa], cc + 1) + b[aa]);
  return d[aa][bb][cc] = ret;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> k;
    for (j = 1; j <= k; j++) {
      te++;
      cin >> a[te] >> b[te];
      c[te] = i;
    }
  }
  n = te;
  memset(d, -1, sizeof(d));
  cout << depe(1, 3, 0) << endl;
}
