#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  long dp[n + 1][10];
  memset(dp, -1, sizeof(dp));
  dp[0][0] = 0;
  for (int i = 0; i < n; i++) {
    int k;
    cin >> k;
    vector<long> a[3];
    for (int j = 0; j < k; j++) {
      int c, d;
      cin >> c >> d;
      c--;
      a[c].push_back(d);
    }
    for (int j = 0; j < 3; j++) sort(a[j].begin(), a[j].end(), greater<long>());
    long b[4][2];
    memset(b, -1, sizeof(b));
    b[0][0] = b[0][1] = 0;
    if (a[0].size() > 0) {
      b[1][0] = max(b[1][0], a[0][0]);
      b[1][1] = max(b[1][1], 2 * a[0][0]);
    }
    if (a[1].size() > 0) {
      b[1][0] = max(b[1][0], a[1][0]);
      b[1][1] = max(b[1][1], 2 * a[1][0]);
    }
    if (a[2].size() > 0) {
      b[1][0] = max(b[1][0], a[2][0]);
      b[1][1] = max(b[1][1], 2 * a[2][0]);
    }
    if (a[0].size() > 1) {
      b[2][0] = max(b[2][0], a[0][0] + a[0][1]);
      b[2][1] = max(b[2][1], 2 * a[0][0] + a[0][1]);
    }
    if (a[0].size() > 0 && a[1].size() > 0) {
      b[2][0] = max(b[2][0], a[0][0] + a[1][0]);
      b[2][1] = max(b[2][1], 2 * a[0][0] + a[1][0]);
      b[2][1] = max(b[2][1], a[0][0] + 2 * a[1][0]);
    }
    if (a[0].size() > 2) {
      b[3][0] = max(b[3][0], a[0][0] + a[0][1] + a[0][2]);
      b[3][1] = max(b[3][1], 2 * a[0][0] + a[0][1] + a[0][2]);
    }
    for (int j = 0; j < 10; j++) {
      if (dp[i][j] == -1) continue;
      for (int l = 0; l < 4; l++) {
        int nx = (j + l) % 10;
        if (nx)
          dp[i + 1][nx] = max(dp[i + 1][nx], dp[i][j] + b[l][0]);
        else
          dp[i + 1][nx] = max(dp[i + 1][nx], dp[i][j] + b[l][1]);
      }
    }
  }
  long ans = 0;
  for (int i = 0; i < 10; i++) ans = max(ans, dp[n][i]);
  cout << ans << '\n';
  return 0;
}
