#include <bits/stdc++.h>
template <class T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
constexpr long long MAX = 5100000;
constexpr long long INF = 1LL << 60;
constexpr int inf = 1 << 28;
constexpr long long mod = 1000000007LL;
using namespace std;
int main() {
  long long n;
  scanf("%lld", &n);
  vector<long long> dp(10, -1);
  dp[0] = 0;
  while (n--) {
    long long k;
    scanf("%lld", &k);
    vector<vector<long long>> a(4);
    for (long long i = 0; i < k; i++) {
      long long c, d;
      scanf("%lld %lld", &c, &d);
      a[c].push_back(d);
    }
    sort(a[1].rbegin(), a[1].rend());
    sort(a[2].rbegin(), a[2].rend());
    sort(a[3].rbegin(), a[3].rend());
    vector<pair<int, long long>> v;
    for (int i = 0; i < 3 && i < a[1].size(); i++) {
      v.emplace_back(1, a[1][i]);
    }
    if (a[2].size()) v.emplace_back(2, a[2][0]);
    sort(v.begin(), v.end());
    vector<long long> ndp(10, -1);
    do {
      vector<vector<vector<long long>>> tmp(
          v.size() + 2,
          vector<vector<long long>>(4, vector<long long>(10, -1)));
      for (long long i = 0; i < 10; i++) {
        chmax(tmp[0][0][i], dp[i]);
      }
      for (int i = 0; i < v.size(); i++) {
        for (int j = 0; j <= 3; j++) {
          for (int k = 0; k < 10; k++) {
            if (tmp[i][j][k] == -1) continue;
            chmax(tmp[i + 1][j][k], tmp[i][j][k]);
            if (j + v[i].first <= 3) {
              if (k == 9) {
                chmax(tmp[i + 1][j + v[i].first][0],
                      tmp[i][j][k] + v[i].second * 2);
              } else {
                chmax(tmp[i + 1][j + v[i].first][k + 1],
                      tmp[i][j][k] + v[i].second);
              }
            }
          }
        }
      }
      for (int i = 0; i <= 3; i++)
        for (int j = 0; j < 10; j++)
          chmax(tmp[v.size() + 1][i][j], tmp[v.size()][i][j]);
      for (int i = 0; i <= 3; i++)
        for (int j = 0; j < 10; j++) chmax(ndp[j], tmp[v.size() + 1][i][j]);
    } while (next_permutation(v.begin(), v.end()));
    if (a[3].size()) {
      for (int i = 8; i > 0; i--) {
        if (dp[i] == -1)
          continue;
        else {
          chmax(dp[i], dp[i - 1] + a[3][0]);
        }
      }
      if (dp[9] != -1) chmax(dp[0], dp[9] + a[3][0] * 2);
    }
    for (int i = 0; i < 10; i++) {
      chmax(dp[i], ndp[i]);
    }
  }
  cout << *max_element(dp.begin(), dp.end()) << endl;
  return 0;
}
