#include <bits/stdc++.h>
using namespace std;
long long dmg[10][10], ant[100], nxt[100], maxi[10][10];
int main() {
  long long n, i, t, j, c, d, q, k, ind, dm, ma, ans;
  cin >> n;
  for (i = 1; i <= 9; i++) {
    ant[i] = -1000000000000000000;
    nxt[i] = -1000000000000000000;
  }
  for (i = 1; i <= n; i++) {
    cin >> t;
    memset(dmg, 0, sizeof(dmg));
    memset(maxi, 0, sizeof(maxi));
    for (j = 1; j <= t; j++) {
      cin >> c >> d;
      for (q = 2; q >= 0; q--)
        for (k = 3; k >= 0; k--) {
          if (dmg[q][k] + d > dmg[q + 1][k + c]) {
            dmg[q + 1][k + c] = dmg[q][k] + d;
            maxi[q + 1][k + c] = max(maxi[q + 1][k + c], d);
          }
        }
    }
    for (k = 0; k <= 3; k++) {
      dmg[k][0] = dmg[k][1];
      maxi[k][0] = maxi[k][1];
      dmg[k][0] = max(dmg[k][1], max(dmg[k][2], dmg[k][3]));
      if (dmg[k][2] > dmg[k][0]) {
        dmg[k][0] = dmg[k][2];
        maxi[k][0] = maxi[k][2];
      }
      if (dmg[k][3] > dmg[k][0]) {
        dmg[k][0] = dmg[k][3];
        maxi[k][0] = maxi[k][3];
      }
    }
    for (j = 0; j <= 9; j++) {
      for (k = 1; k <= 3; k++) {
        ind = j + k;
        dm = dmg[k][0];
        ma = maxi[k][0];
        if (ind >= 10) {
          dm += ma;
          ind -= 10;
        }
        nxt[ind] = max(nxt[ind], dm + ant[j]);
      }
    }
    for (j = 0; j <= 9; j++) {
      ant[j] = nxt[j];
      nxt[j] = -1000000000000000000;
    }
  }
  ans = ant[0];
  for (i = 1; i <= 9; i++) ans = max(ans, ant[i]);
  cout << ans << '\n';
}
