#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline void ckmax(T& x, T y) {
  x = (y > x ? y : x);
}
template <typename T>
inline void ckmin(T& x, T y) {
  x = (y < x ? y : x);
}
const int MAXN = 2e5, P = 10, C = 3;
const long long LL_INF = 1e18;
int n, m;
struct Card_t {
  int c, d;
};
Card_t a[MAXN + 5];
long long dp[MAXN + 5][P];
int curi;
void solve(int l, int r, vector<vector<long long> >& curdp) {
  if (l == r) {
    for (int j = 0; j < C; ++j) {
      for (int k = 0; k + a[l].c <= C; ++k)
        if (curdp[j][k] != -LL_INF) {
          for (int o = 0; o < P; ++o)
            if (dp[curi - 1][o] != -LL_INF) {
              ckmax(dp[curi][(o + j + 1) % P],
                    dp[curi - 1][o] + curdp[j][k] +
                        a[l].d * (((o + j + 1) % P == 0) ? 2 : 1));
            }
        }
    }
    return;
  }
  int mid = (l + r) >> 1;
  vector<vector<long long> > mem = curdp;
  for (int i = l; i <= mid; ++i) {
    for (int j = C - 1; j >= 1; --j) {
      for (int k = C - 1; k >= a[i].c; --k)
        if (curdp[j - 1][k - a[i].c] != -LL_INF) {
          ckmax(curdp[j][k], curdp[j - 1][k - a[i].c] + a[i].d);
        }
    }
  }
  solve(mid + 1, r, curdp);
  curdp = mem;
  for (int i = mid + 1; i <= r; ++i) {
    for (int j = C - 1; j >= 1; --j) {
      for (int k = C - 1; k >= a[i].c; --k)
        if (curdp[j - 1][k - a[i].c] != -LL_INF) {
          ckmax(curdp[j][k], curdp[j - 1][k - a[i].c] + a[i].d);
        }
    }
  }
  solve(l, mid, curdp);
}
int main() {
  cin >> n;
  dp[0][0] = 0;
  for (int j = 1; j < P; ++j) dp[0][j] = -LL_INF;
  for (int i = 1; i <= n; ++i) {
    cin >> m;
    for (int j = 1; j <= m; ++j) {
      cin >> a[j].c >> a[j].d;
    }
    for (int j = 0; j < P; ++j) {
      dp[i][j] = dp[i - 1][j];
    }
    vector<vector<long long> > curdp;
    curdp.resize(C);
    for (int j = 0; j < C; ++j) {
      curdp[j].resize(C);
      for (int k = 0; k < C; ++k) {
        curdp[j][k] = -LL_INF;
      }
    }
    curdp[0][0] = 0;
    curi = i;
    solve(1, m, curdp);
  }
  long long ans = -LL_INF;
  for (int j = 0; j < P; ++j) ckmax(ans, dp[n][j]);
  cout << ans << endl;
  return 0;
}
