#include <bits/stdc++.h>
using namespace std;
signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long n;
  cin >> n;
  pair<long long, long long> dp[n + 1][7];
  memset(dp, 0, sizeof(dp));
  for (long long j = 0; j < n; j++) {
    long long k;
    cin >> k;
    vector<long long> v[4];
    for (long long i = 0; i < k; i++) {
      long long c, d;
      cin >> c >> d;
      v[c].push_back(d);
    }
    for (long long i = 1; i <= 3; i++) {
      sort(v[i].begin(), v[i].end());
      reverse(v[i].begin(), v[i].end());
    }
    if ((long long)v[1].size()) {
      for (long long i = 1; i <= 6; i++) {
        dp[j + 1][1] = max(dp[j][i], dp[j + 1][1]);
      }
      dp[j + 1][1].second++;
      dp[j + 1][1].first += v[1][0];
      if ((dp[j + 1][1].second - 1) / 10 != dp[j + 1][1].second / 10)
        dp[j + 1][1].first += v[1][0];
    }
    if ((long long)v[1].size() >= 2) {
      for (long long i = 1; i <= 6; i++) {
        dp[j + 1][2] = max(dp[j][i], dp[j + 1][2]);
      }
      dp[j + 1][2].second += 2;
      dp[j + 1][2].first += v[1][0] + v[1][1];
      if ((dp[j + 1][2].second - 2) / 10 != dp[j + 1][2].second / 10)
        dp[j + 1][2].first += v[1][0];
    }
    if ((long long)v[2].size()) {
      for (long long i = 1; i <= 6; i++) {
        dp[j + 1][3] = max(dp[j][i], dp[j + 1][3]);
      }
      dp[j + 1][3].second++;
      dp[j + 1][3].first += v[2][0];
      if ((dp[j + 1][3].second - 1) / 10 != dp[j + 1][3].second / 10)
        dp[j + 1][3].first += v[2][0];
    }
    if ((long long)v[1].size() >= 3) {
      for (long long i = 1; i <= 6; i++) {
        if (dp[j][i].first + v[1][0] + v[1][1] + v[1][2] > dp[j + 1][4].first)
          dp[j + 1][4].second = dp[j][i].second;
        dp[j + 1][4].first = max(dp[j][i].first + v[1][0] + v[1][1] + v[1][2],
                                 dp[j + 1][4].first);
      }
      dp[j + 1][4].second += 3;
      if ((dp[j + 1][4].second - 3) / 10 != dp[j + 1][4].second / 10)
        dp[j + 1][4].first += v[1][0];
    }
    if ((long long)v[1].size() and (long long) v[2].size()) {
      for (long long i = 1; i <= 6; i++) {
        if (dp[j][i].first + v[1][0] + v[2][0] > dp[j + 1][5].first)
          dp[j + 1][5].second = 2 + dp[j][i].second;
        dp[j + 1][5].first =
            max(dp[j][i].first + v[1][0] + v[2][0], dp[j + 1][5].first);
      }
      if ((dp[j + 1][5].second - 2) / 10 != dp[j + 1][5].second / 10)
        dp[j + 1][5].first += max(v[1][0], v[2][0]);
    }
    if ((long long)v[3].size()) {
      for (long long i = 1; i <= 6; i++) {
        if (dp[j][i].first + v[3][0] > dp[j + 1][6].first)
          dp[j + 1][6].first = 1 + dp[j][i].second;
        dp[j + 1][6].first = max(dp[j][i].first + v[3][0], dp[j + 1][6].first);
      }
      if ((dp[j + 1][6].second - 1) / 10 != dp[j + 1][6].second / 10)
        dp[j + 1][6].first += v[3][0];
    }
    for (long long i = 1; i <= 6; i++)
      dp[j + 1][i] = max(dp[j][i], dp[j + 1][i]);
  }
  long long ans = 0;
  for (long long i = 1; i <= 6; i++) ans = max(ans, dp[n][i].first);
  cout << ans;
  return 0;
}
