#include <bits/stdc++.h>
const int N = 200005;
using namespace std;
long long n, k, indx, type, value, dp[2][N];
vector<int> vc[4];
void clr() {
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < 2; j++) dp[j][i] = -999999999;
  }
  dp[1][0] = 0;
}
int main() {
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  ;
  clr();
  cin >> n;
  for (int i = 0; i < n; i++) {
    int t = i % 2;
    for (int j = 0; j <= 3; j++) {
      vc[j].clear();
      for (int q = 0; q < 3; q++) {
        vc[j].push_back(-999999999);
      }
    }
    cin >> k;
    for (int j = 0; j < k; j++) {
      cin >> type >> value;
      vc[type].push_back(value);
    }
    for (int j = 1; j <= 3; j++) sort((vc[j]).begin(), (vc[j]).end());
    for (int j = 0; j <= 10; j++) {
      dp[t][j + 1] = max(dp[t][j + 1], dp[t ^ 1][j] + vc[1].back());
      if (j + 1 >= 10) {
        indx = (j + 1) == 10 ? 10 : (j + 1) % 11 + 1;
        dp[t][indx] =
            max(dp[t][indx],
                dp[t ^ 1][j] + (j + 1 == 10 ? vc[1].back() * 2 : vc[1].back()));
      }
    }
    for (int j = 0; j <= 10; j++) {
      dp[t][j + 1] = max(dp[t][j + 1], dp[t ^ 1][j] + vc[2].back());
      if (j + 1 >= 10) {
        indx = (j + 1) == 10 ? 10 : (j + 1) % 11 + 1;
        dp[t][indx] = max(dp[t][indx], dp[t ^ 1][j] + vc[2].back() * 2);
      }
      dp[t][j + 2] =
          max(dp[t][j + 2], dp[t ^ 1][j] + vc[1][(int)(vc[1]).size() - 1] +
                                vc[1][(int)(vc[1]).size() - 2]);
      if (j + 2 >= 10) {
        indx = (j + 2) == 10 ? 10 : (j + 2) % 11 + 1;
        dp[t][indx] =
            max(dp[t][indx], dp[t ^ 1][j] + vc[1][(int)(vc[1]).size() - 1] +
                                 vc[1][(int)(vc[1]).size() - 2] +
                                 vc[1][(int)(vc[1]).size() - 1] * 2);
      }
    }
    for (int j = 0; j <= 10; j++) {
      dp[t][j + 1] = max(dp[t][j + 1], dp[t ^ 1][j] + vc[3].back());
      if (j + 1 >= 10) {
        indx = (j + 1) == 10 ? 10 : (j + 1) % 11 + 1;
        dp[t][indx] = max(dp[t][indx], dp[t ^ 1][j] + vc[3].back() * 2);
      }
      dp[t][j + 2] =
          max(dp[t][j + 2], dp[t ^ 1][j] + vc[1][(int)(vc[1]).size() - 1] +
                                vc[2][(int)(vc[2]).size() - 1]);
      if (j + 2 >= 10) {
        indx = (j + 2) == 10 ? 10 : (j + 2) % 11 + 1;
        dp[t][indx] = max(dp[t][indx], dp[t ^ 1][j] +
                                           max(vc[1][(int)(vc[1]).size() - 1],
                                               vc[2][(int)(vc[2]).size() - 1]) +
                                           vc[1][(int)(vc[1]).size() - 1] +
                                           vc[2][(int)(vc[2]).size() - 1]);
      }
      dp[t][j + 3] =
          max(dp[t][j + 3], dp[t ^ 1][j] + vc[1][(int)(vc[1]).size() - 1] +
                                vc[1][(int)(vc[1]).size() - 2] +
                                vc[1][(int)(vc[1]).size() - 3]);
      if (j + 3 >= 10) {
        indx = (j + 3) == 10 ? 10 : (j + 3) % 11 + 1;
        dp[t][indx] =
            max(dp[t][indx], dp[t ^ 1][j] + vc[1][(int)(vc[1]).size() - 1] * 2 +
                                 vc[1][(int)(vc[1]).size() - 2] +
                                 vc[1][(int)(vc[1]).size() - 3]);
      }
    }
    for (int j = 0; j <= 10; j++) {
      dp[t ^ 1][j] = max(dp[t ^ 1][j], dp[t][j]);
    }
  }
  long long ans = -999999999;
  for (int j = 0; j <= 10; j++) {
    ans = max({ans, dp[0][j], dp[1][j]});
  }
  cout << ans << "\n";
  return 0;
}
