#include <bits/stdc++.h>
using namespace std;
const int mxN = 2e5 + 5;
const long long inf = 1e18;
int n;
long long dp[mxN][11];
int main() {
  ios ::sync_with_stdio(false), cin.tie(nullptr);
  cin >> n;
  for (int i = 0; i <= n; ++i) {
    for (int j = 0; j < 10; ++j) dp[i][j] = -inf;
  }
  dp[0][0] = 0;
  for (int i = 1; i <= n; ++i) {
    int k;
    cin >> k;
    vector<long long> vec[4];
    vector<vector<long long> > D(4, vector<long long>(4, -inf));
    for (int j = 0; j < k; ++j) {
      long long c, d;
      cin >> c >> d;
      vec[c].push_back(d);
    }
    for (int j = 1; j < 4; ++j) {
      sort(vec[j].begin(), vec[j].end());
      reverse(vec[j].begin(), vec[j].end());
    }
    if (!vec[1].empty()) {
      if (!vec[2].empty()) D[2][3] = vec[1][0] + vec[2][0];
      if (vec[1].size() > 1) {
        long long sum = 0;
        for (int j = 0; j < min((int)vec[1].size(), 3); ++j) sum += vec[1][j];
        int cnt = min((int)vec[1].size(), 3);
        D[cnt][cnt] = sum;
      }
      if (vec[2].empty() && vec[1].size() == 1) D[1][1] = vec[1][0];
    }
    if (vec[1].empty() && !vec[2].empty()) D[1][2] = vec[2][0];
    if (!vec[3].empty()) D[1][3] = vec[3][0];
    for (int games = 1; games <= 3; ++games) {
      for (int cost = 1; cost <= 3; ++cost) {
        for (int j = 0; j < 10; ++j) {
          if (!((j + games) % 10))
            dp[i][0] = max(dp[i][0], dp[i - 1][j] + D[games][cost] * 2);
          else
            dp[i][(j + games) % 10] =
                max(dp[i][(j + games) % 10], dp[i - 1][j] + D[games][cost]);
        }
      }
    }
  }
  long long ans = 0;
  for (int i = 0; i < 10; ++i) ans = max(ans, dp[n][i]);
  cout << ans;
  return 0;
}
