#include <bits/stdc++.h>
using namespace std;
long long N, dp[2][4][10], K[200005];
vector<long long> C[200005];
vector<long long> D[200005];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> N;
  for (int i = 1; i <= N; ++i) {
    cin >> K[i];
    C[i].resize(K[i] + 3, 0);
    D[i].resize(K[i] + 3, 0);
    for (int j = 1; j <= K[i]; ++j) {
      cin >> C[i][j] >> D[i][j];
    }
  }
  for (int i = 0; i < 2; ++i) {
    for (int j = 0; j < 4; ++j) {
      for (int k = 0; k < 10; ++k) {
        dp[i][j][k] = -1e18;
      }
    }
  }
  dp[0][0][0] = 0;
  int cnt = 0;
  for (int i = 1; i <= N; ++i) {
    for (int j = 1; j <= K[i]; ++j) {
      cnt++;
      for (int c = 0; c < 4; ++c) {
        for (int m = 0; m < 10; ++m) {
          dp[cnt & 1][c][m] = dp[(cnt - 1) & 1][c][m];
          if (c >= C[i][j]) {
            dp[cnt & 1][c][m] =
                max(dp[cnt & 1][c][m],
                    dp[(cnt - 1) & 1][c - C[i][j]][(m - 1 + 10) % 10] +
                        (m == 0 ? 2 * D[i][j] : D[i][j]));
          }
        }
      }
    }
    for (int m = 0; m < 10; ++m) {
      dp[cnt & 1][0][m] = max(max(dp[cnt & 1][0][m], dp[cnt & 1][1][m]),
                              max(dp[cnt & 1][2][m], dp[cnt & 1][3][m]));
    }
  }
  long long ans = 0;
  for (int c = 0; c < 4; ++c) {
    for (int m = 0; m < 10; ++m) {
      ans = max(ans, dp[cnt & 1][c][m]);
    }
  }
  cout << ans << '\n';
}
