#include <bits/stdc++.h>
const int N = 200005;
using namespace std;
long long n, k, sum, indx, type, value, dp[2][N];
vector<long long> vc[4];
void clr() {
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < 2; j++) dp[j][i] = -3999999999999999999;
  }
  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++) {
      indx = j + 1;
      sum = vc[1].back();
      if (indx >= 10) {
        indx = indx % 10;
        sum += vc[1].back();
      }
      dp[t][indx] = max(dp[t][indx], dp[t ^ 1][j] + sum);
    }
    for (int j = 0; j < 10; j++) {
      indx = j + 1;
      sum = vc[2].back();
      if (indx >= 10) {
        indx = indx % 10;
        sum += vc[2].back();
      }
      dp[t][indx] = max(dp[t][indx], dp[t ^ 1][j] + sum);
      indx = j + 2;
      sum = vc[1].back() + vc[1][(int)(vc[1]).size() - 2];
      if (indx >= 10) {
        indx = indx % 10;
        sum += vc[1].back();
      }
      dp[t][indx] = max(dp[t][indx], dp[t ^ 1][j] + sum);
    }
    for (int j = 0; j < 10; j++) {
      indx = j + 1;
      sum = vc[3].back();
      if (indx >= 10) {
        indx = indx % 10;
        sum += vc[3].back();
      }
      dp[t][indx] = max(dp[t][indx], dp[t ^ 1][j] + sum);
      indx = j + 2;
      sum = vc[2].back() + vc[1].back();
      if (indx >= 10) {
        indx = indx % 10;
        sum += max(vc[2].back(), vc[1].back());
      }
      dp[t][indx] = max(dp[t][indx], dp[t ^ 1][j] + sum);
      indx = j + 3;
      sum = vc[1].back() + vc[1][(int)(vc[1]).size() - 2] +
            vc[1][(int)(vc[1]).size() - 3];
      if (indx >= 10) {
        indx = indx % 10;
        sum += vc[1].back();
      }
      dp[t][indx] = max(dp[t][indx], dp[t ^ 1][j] + sum);
    }
    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;
}
