#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  int cur_idx = 0;
  long long dp[200100] = {0}, cur[200100] = {0};
  for (int i = 0; i < n; i++) {
    int k;
    cin >> k;
    vector<long long> v[4];
    for (int j = 0; j < k; j++) {
      long long c, d;
      cin >> c >> d;
      v[c].push_back(d);
    }
    sort(v[1].begin(), v[1].end(), greater<long long>());
    sort(v[2].begin(), v[2].end(), greater<long long>());
    sort(v[3].begin(), v[3].end(), greater<long long>());
    int tmp_idx = cur_idx;
    if ((int)v[3].size() != 0) {
      for (int j = cur_idx; j >= 0; j--) {
        if ((j + 1) % 10 == 0)
          cur[j + 1] = max(cur[j + 1], dp[j] + v[3][0] * 2LL);
        else
          cur[j + 1] = max(cur[j + 1], dp[j] + v[3][0]);
      }
      tmp_idx = cur_idx + 1;
    }
    if ((int)v[2].size() != 0) {
      for (int j = cur_idx; j >= 0; j--) {
        if ((j + 1) % 10 == 0)
          cur[j + 1] = max(cur[j + 1], dp[j] + v[2][0] * 2LL);
        else
          cur[j + 1] = max(cur[j + 1], dp[j] + v[2][0]);
      }
      tmp_idx = cur_idx + 1;
      if ((int)v[1].size() != 0) {
        for (int j = cur_idx; j >= 0; j--) {
          if ((j + 2) % 10 == 0)
            cur[j + 2] = max(cur[j + 2], dp[j] + max(v[2][0] * 2LL + v[1][0],
                                                     v[2][0] + v[1][0] * 2LL));
          else
            cur[j + 2] = max(cur[j + 2], dp[j] + v[2][0] + v[1][0]);
        }
        tmp_idx = max(tmp_idx, cur_idx + 2);
      }
    }
    if ((int)v[1].size() != 0) {
      if ((int)v[1].size() >= 3) {
        for (int j = cur_idx; j >= 0; j--) {
          if ((j + 3) % 10 == 0)
            cur[j + 3] =
                max(cur[j + 3], dp[j] + v[1][0] * 2LL + v[1][1] + v[1][2]);
          else
            cur[j + 3] = max(cur[j + 3], dp[j] + v[1][0] + v[1][1] + v[1][2]);
        }
        tmp_idx = max(tmp_idx, cur_idx + 3);
      } else if ((int)v[1].size() == 2) {
        for (int j = cur_idx; j >= 0; j--) {
          if ((j + 2) % 10 == 0)
            cur[j + 2] = max(cur[j + 2], dp[j] + v[1][0] * 2LL + v[1][1]);
          else
            cur[j + 2] = max(cur[j + 2], dp[j] + v[1][0] + v[1][1]);
        }
        tmp_idx = max(tmp_idx, cur_idx + 2);
      } else if ((int)v[1].size() == 1) {
        for (int j = cur_idx; j >= 0; j--) {
          if ((j + 1) % 10 == 0)
            cur[j + 1] = max(cur[j + 1], dp[j] + v[1][0] * 2LL);
          else
            cur[j + 1] = max(cur[j + 1], dp[j] + v[1][0]);
        }
        tmp_idx = max(tmp_idx, cur_idx + 1);
      }
    }
    memcpy(dp, cur, sizeof(dp));
    cur_idx = tmp_idx;
  }
  long long ans = 0;
  for (auto i : dp) {
    ans = max(ans, i);
  }
  cout << ans << '\n';
  return 0;
}
