#include <bits/stdc++.h>
using namespace std;
const int MOD = 10;
const int MAX = 2e5 + 10;
int dp[MAX][10];
int main() {
  int n;
  cin >> n;
  for (int i = 0; i <= MAX; i++) {
    for (int j = 0; j < 10; j++) dp[i][j] = -1;
  }
  dp[0][0] = 0;
  for (int i = 1; i <= n; i++) {
    int k, c, d;
    int L1 = 0, L2 = 0, L3 = 0;
    int c1[5] = {0, 0, 0, 0, 0}, c2 = 0, c3 = 0;
    cin >> k;
    for (int j = 1; j <= k; j++) {
      cin >> c >> d;
      if (c == 1) {
        if (L1 == 3) {
          int x = 1;
          if (c1[x] > c1[2]) x = 2;
          if (c1[x] > c1[3]) x = 3;
          if (c1[x] < d) c1[x] = d;
        } else
          c1[++L1] = d;
      } else if (c == 2) {
        L2 = 1;
        if (d > c2) c2 = d;
      } else {
        L3 = 1;
        if (d > c3) c3 = d;
      }
    }
    sort(c1 + 1, c1 + 1 + 3);
    int MAX_V = max(max(c1[3], c2), c3);
    int two_fir = c1[3];
    int two_sec = c1[2];
    if (c2 > two_fir)
      two_fir = c2;
    else if (c2 > two_sec)
      two_sec = c2;
    int three_sum = c1[1] + c1[2] + c1[3];
    for (int j = 0; j < 10; j++) dp[i][j] = dp[i - 1][j];
    for (int j = 0; j < 10; j++) {
      if (dp[i - 1][j] != -1) {
        if (L1 + L2 + L3 >= 1) {
          dp[i][((j + 1) % (MOD))] =
              max(dp[i][((j + 1) % (MOD))],
                  dp[i - 1][j] + MAX_V + (j + 1 >= 10) * MAX_V);
        }
        if (L1 + L2 >= 2) {
          dp[i][((j + 2) % (MOD))] =
              max(dp[i][((j + 2) % (MOD))],
                  dp[i - 1][j] + two_fir + two_sec + (j + 2 >= 10) * two_fir);
        }
        if (L1 >= 3) {
          dp[i][((j + 3) % (MOD))] =
              max(dp[i][((j + 3) % (MOD))],
                  dp[i - 1][j] + three_sum + (j + 3 >= 10) * c1[3]);
        }
      }
    }
  }
  int haha = 0;
  for (int i = 0; i <= 9; i++) haha = max(haha, dp[n][i]);
  cout << haha << endl;
  return 0;
}
