#include <bits/stdc++.h>
using namespace std;
template <class T>
constexpr T lcm(T m, T n) {
  return m / __gcd(m, n) * n;
}
template <typename I>
void join(ostream &ost, I s, I t, string d = " ") {
  for (auto i = s; i != t; ++i) {
    if (i != s) ost << d;
    ost << *i;
  }
  ost << endl;
}
template <typename T>
istream &operator>>(istream &is, vector<T> &v) {
  for (auto &a : v) is >> a;
  return is;
}
template <typename T, typename U>
istream &operator>>(istream &is, pair<T, U> &p) {
  is >> p.first >> p.second;
  return is;
}
template <typename T, typename U>
bool chmin(T &a, const U &b) {
  return (a > b ? a = b, true : false);
}
template <typename T, typename U>
bool chmax(T &a, const U &b) {
  return (a < b ? a = b, true : false);
}
template <typename T, size_t N, typename U>
void fill_array(T (&a)[N], const U &v) {
  fill((U *)a, (U *)(a + N), v);
}
long long int dp[202000][10];
const long long int inf = LLONG_MAX / 10;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N;
  while (cin >> N) {
    fill_array(dp, -inf);
    dp[0][0] = 0;
    for (long long int i = (0); i < (N); ++i) {
      int k;
      cin >> k;
      vector<int> c1, c2, c3;
      for (long long int j = (0); j < (k); ++j) {
        int c, d;
        cin >> c >> d;
        if (c == 1)
          c1.push_back(d);
        else if (c == 2)
          c2.push_back(d);
        else
          c3.push_back(d);
      }
      sort((c1).rbegin(), (c1).rend());
      sort((c2).rbegin(), (c2).rend());
      sort((c3).rbegin(), (c3).rend());
      for (long long int j = (0); j < (10); ++j) {
        if (c1.size())
          chmax(dp[i + 1][(j + 1) % 10], dp[i][j] + c1[0] * (j == 9 ? 2 : 1));
        if (c2.size())
          chmax(dp[i + 1][(j + 1) % 10], dp[i][j] + c2[0] * (j == 9 ? 2 : 1));
        if (c3.size())
          chmax(dp[i + 1][(j + 1) % 10], dp[i][j] + c3[0] * (j == 9 ? 2 : 1));
        if (c1.size() >= 2) {
          chmax(dp[i + 1][(j + 2) % 10],
                dp[i][j] + c1[0] * (j >= 8 ? 2 : 1) + c1[1]);
        }
        if (c1.size() and c2.size()) {
          long long int t = c1[0] + c2[0];
          if (j >= 8) {
            if (c1[0] > c2[0])
              t = c1[0] * 2 + c2[0];
            else
              t = c1[0] + c2[0] * 2;
          }
          chmax(dp[i + 1][(j + 2) % 10], dp[i][j] + t);
        }
        if (c1.size() >= 3) {
          chmax(dp[i + 1][(j + 3) % 10],
                dp[i][j] + c1[0] * (j >= 7 ? 2 : 1) + c1[1] + c1[2]);
        }
      }
    }
    long long int ans = 0;
    for (long long int i = (0); i < (10); ++i) chmax(ans, dp[N][i]);
    cout << ans << endl;
  }
  return 0;
}
