#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<pair<long long, long long>> v[n][3];
  for (int i = 0; i < n; i++) {
    int m;
    cin >> m;
    long long two = -1;
    long long three = -1;
    long long one = -1;
    vector<long long> t;
    for (int j = 0; j < m; j++) {
      long long a, b;
      cin >> a >> b;
      if (a == 1 && b > one) one = b;
      if (a == 2 && b > two) two = b;
      if (a == 3 && b > three) three = b;
      if (a == 1) t.push_back(b);
    }
    sort(t.begin(), t.end());
    reverse(t.begin(), t.end());
    v[i][0].push_back({max(one, max(two, three)), max(one, max(two, three))});
    if (t.size() >= 2) v[i][1].push_back({t[0] + t[1], t[0]});
    if (one != -1 && two != -1) v[i][1].push_back({one + two, max(one, two)});
    if (t.size() >= 3) v[i][2].push_back({t[0] + t[1] + t[2], t[0]});
  }
  long long dp[n][10];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 10; j++) dp[i][j] = -1;
  }
  dp[0][0] = 0;
  dp[0][1] = v[0][0][0].first;
  if (v[0][1].size() != 0) {
    dp[0][2] = v[0][1][0].first;
    if (v[0][1].size() > 1) dp[0][2] = max(dp[0][2], v[0][1][1].first);
  }
  if (v[0][2].size() != 0) dp[0][3] = v[0][2][0].first;
  for (int i = 1; i < n; i++) {
    for (int j = 0; j < 10; j++) {
      if (dp[i - 1][j] == -1) continue;
      dp[i][(j + 1) % 10] =
          max(dp[i][(j + 1) % 10], dp[i - 1][j] + v[i][0][0].first +
                                       (j >= 9 ? v[i][0][0].second : 0));
    }
    if (v[i][1].size() != 0) {
      for (int j = 0; j < 10; j++) {
        if (dp[i - 1][j] == -1) continue;
        pair<int, int> p = v[i][1][0];
        dp[i][(j + 2) % 10] =
            max(dp[i][(i + 2) % 10],
                dp[i - 1][j] + p.first + (j >= 8 ? p.second : 0));
        if (v[i][1].size() > 1) {
          pair<int, int> p = v[i][1][0];
          dp[i][(j + 2) % 10] =
              max(dp[i][(j + 2) % 10],
                  dp[i - 1][j] + p.first + (j >= 8 ? p.second : 0));
        }
      }
    }
    if (v[i][2].size() != 0) {
      for (int j = 0; j < 10; j++) {
        if (dp[i - 1][j] == -1) continue;
        dp[i][(j + 3) % 10] =
            max(dp[i][(j + 3) % 10], dp[i - 1][j] + v[i][2][0].first +
                                         (j >= 7 ? v[i][2][0].second : 0));
      }
    }
  }
  long long ans = -1;
  for (int j = 0; j < 10; j++) ans = max(ans, dp[n - 1][j]);
  cout << ans;
}
