#include <bits/stdc++.h>
const long long int MOD = 1e9 + 7;
using namespace std;
int n;
std::vector<long long int> adj[200005][4];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int k;
    cin >> k;
    std::vector<long long int> v[4];
    for (int j = 0; j < k; j++) {
      long long int c, d;
      cin >> c >> d;
      v[c].push_back(d);
    }
    for (int j = 1; j < 4; j++)
      sort(v[j].begin(), v[j].end(), greater<long long int>());
    if (v[1].size() >= 3) {
      adj[i][3].push_back(v[1][0]);
      adj[i][3].push_back(v[1][1]);
      adj[i][3].push_back(v[1][2]);
    }
    if (v[1].size() >= 2 || (v[1].size() >= 1 && v[2].size() >= 1)) {
      long long int a = v[1][0], b;
      if (v[1].size() >= 2 && v[2].size() >= 1)
        b = max(v[1][1], v[2][0]);
      else if (v[1].size() >= 2)
        b = v[1][1];
      else
        b = v[2][0];
      adj[i][2].push_back(a);
      adj[i][2].push_back(b);
    }
    if (v[1].size() >= 1 || v[2].size() >= 1 || v[3].size() >= 1) {
      v[1].push_back(0);
      v[2].push_back(0);
      v[3].push_back(0);
      adj[i][1].push_back(max(v[1][0], max(v[2][0], v[3][0])));
    }
  }
  long long int dp[n + 3][11];
  memset(dp, 0, sizeof(dp));
  if (adj[0][1].size() >= 1) {
    dp[0][1] = adj[0][1][0];
  }
  if (adj[0][2].size() >= 2) {
    dp[0][2] = adj[0][2][0] + adj[0][2][1];
  }
  if (adj[0][3].size() >= 3) {
    dp[0][3] = adj[0][3][0] + adj[0][3][1] + adj[0][3][2];
  }
  for (int i = 1; i < n; i++) {
    for (int j = 0; j < 10; j++) dp[i][j] = dp[i - 1][j];
    if (adj[i][1].size() >= 1) {
      for (int j = 0; j < 9; j++) {
        if (j == 0)
          dp[i][j + 1] = max(dp[i][j + 1], dp[i - 1][j] + adj[i][1][0]);
        else if (dp[i - 1][j] != 0)
          dp[i][j + 1] = max(dp[i][j + 1], dp[i - 1][j] + adj[i][1][0]);
      }
      if (dp[i - 1][9] != 0)
        dp[i][0] = max(dp[i][0], dp[i - 1][9] + 2 * adj[i][1][0]);
    }
    if (adj[i][2].size() >= 2) {
      for (int j = 0; j < 8; j++) {
        if (j == 0)
          dp[i][j + 2] =
              max(dp[i][j + 2], dp[i - 1][j] + adj[i][2][0] + adj[i][2][1]);
        else if (dp[i - 1][j] != 0)
          dp[i][j + 2] =
              max(dp[i][j + 2], dp[i - 1][j] + adj[i][2][0] + adj[i][2][1]);
      }
      long long int a = max(adj[i][2][0], adj[i][2][1]),
                    b = min(adj[i][2][0], adj[i][2][1]);
      if (dp[i - 1][8] != 0) dp[i][0] = max(dp[i][0], dp[i - 1][8] + 2 * a + b);
      if (dp[i - 1][9] != 0) dp[i][1] = max(dp[i][1], dp[i - 1][9] + 2 * a + b);
    }
    if (adj[i][3].size() >= 3) {
      for (int j = 0; j < 7; j++) {
        if (j == 0)
          dp[i][j + 3] = max(dp[i][j + 3], dp[i - 1][j] + adj[i][3][0] +
                                               adj[i][3][1] + adj[i][3][2]);
        else if (dp[i - 1][j] != 0)
          dp[i][j + 3] = max(dp[i][j + 3], dp[i - 1][j] + adj[i][3][0] +
                                               adj[i][3][1] + adj[i][3][2]);
      }
      std::vector<long long int> v;
      for (int j = 0; j < 3; j++) v.push_back(adj[i][3][j]);
      sort(v.begin(), v.end());
      long long int a = v[2], b = v[0] + v[1];
      if (dp[i - 1][7] != 0) dp[i][0] = max(dp[i][0], dp[i - 1][7] + 2 * a + b);
      if (dp[i - 1][8] != 0) dp[i][1] = max(dp[i][1], dp[i - 1][8] + 2 * a + b);
      if (dp[i - 1][9] != 0) dp[i][2] = max(dp[i][1], dp[i - 1][9] + 2 * a + b);
    }
  }
  long long int ans = 0;
  for (int j = 0; j <= 9; j++) ans = max(ans, dp[n - 1][j]);
  cout << ans << endl;
  return 0;
}
