#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<long long> dp(10, INT64_MIN / 16), tp(10, 0);
  dp[0] = 0;
  for (int i = 0; i < n; i++) {
    int k;
    cin >> k;
    long long m11 = INT64_MIN / 16, m12 = INT64_MIN / 16, m13 = INT64_MIN / 16,
              m20 = INT64_MIN / 16, m30 = INT64_MIN / 16;
    for (int j = 0; j < k; j++) {
      long long cost, damage;
      cin >> cost >> damage;
      if (cost == 1 && damage > m11) {
        m13 = m12;
        m12 = m11;
        m11 = damage;
      } else if (cost == 1 && damage > m12) {
        m13 = m12;
        m12 = damage;
      } else if (cost == 1 && damage > m13) {
        m13 = damage;
      } else if (cost == 2 && damage > m20) {
        m20 = damage;
      } else if (cost == 3 && damage > m30) {
        m30 = damage;
      }
    }
    long long d1 = max(m11, max(m20, m30)), d2 = max(m11 + m12, m11 + m20),
              d3 = m11 + m12 + m13;
    long long b1 = m11, b2 = m12 > m20 ? m11 : max(m11, m20), b3 = d1;
    tp = vector<long long>(dp);
    for (int j = 0; j < 10; j++) {
      if (d1 > 0)
        tp[(j + 1) % 10] =
            max(tp[(j + 1) % 10], d1 + dp[j] + (j + 1 >= 10 ? b1 : 0));
      if (d2 > 0)
        tp[(j + 2) % 10] =
            max(tp[(j + 2) % 10], d2 + dp[j] + (j + 1 >= 10 ? b2 : 0));
      if (d3 > 0)
        tp[(j + 3) % 10] =
            max(tp[(j + 3) % 10], d3 + dp[j] + (j + 1 >= 10 ? b3 : 0));
    }
    dp = tp;
  }
  cout << *max_element(dp.begin(), dp.end()) << endl;
}
