#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
long long dp[maxn][10];
int main() {
  dp[0][0] = -1;
  int n;
  scanf("%d", &n);
  int k;
  long long a, b;
  long long tr, dr;
  vector<long long> pr;
  long long sum;
  for (int i = 0; i < n; i++) {
    scanf("%d", &k);
    pr.clear();
    dr = 0;
    tr = 0;
    for (int j = 0; j < k; j++) {
      scanf("%lld%lld", &a, &b);
      if (a == 1) {
        pr.push_back(b);
      } else if (a == 2) {
        dr = max(dr, b);
      } else {
        tr = max(tr, b);
      }
    }
    sort(pr.begin(), pr.end());
    for (int j = 0; j < 10; j++) {
      if (dp[i][j] != 0) {
        if (i == 0 && j == 0) {
          dp[i][j] = 0;
        }
        if (j == 9) {
          sum = max(tr * 2, dr * 2);
          if (!pr.empty()) {
            sum = max(sum, pr[pr.size() - 1] * 2);
          }
          dp[i + 1][0] = max(dp[i + 1][0], max(dp[i][0], dp[i][j] + sum));
        } else {
          sum = max(tr, dr);
          if (!pr.empty()) {
            sum = max(sum, pr[pr.size() - 1]);
          }
          dp[i + 1][j + 1] =
              max(dp[i + 1][j + 1], max(dp[i][j + 1], dp[i][j] + sum));
        }
        if (j > 7 && !pr.empty()) {
          sum = max(dr, pr[pr.size() - 1]) * 2 + min(dr, pr[pr.size() - 1]);
          if (pr.size() > 1) {
            sum = max(sum, pr[pr.size() - 1] * 2 + pr[pr.size() - 2]);
          }
          dp[i + 1][(j + 2) % 10] =
              max(dp[i + 1][(j + 2) % 10],
                  max(dp[i][(j + 2) % 10], dp[i][j] + sum));
        } else if (!pr.empty()) {
          sum = dr + pr[pr.size() - 1];
          if (pr.size() > 1) {
            sum = max(sum, pr[pr.size() - 1] + pr[pr.size() - 2]);
          }
          dp[i + 1][j + 2] =
              max(dp[i + 1][j + 2], max(dp[i][j + 2], dp[i][j] + sum));
        } else {
          dp[i + 1][(j + 2) % 10] =
              max(dp[i][(j + 2) % 10], dp[i + 1][(j + 2) % 10]);
        }
        if (j > 6 && pr.size() > 2) {
          dp[i + 1][(j + 3) % 10] =
              max(dp[i + 1][(j + 3) % 10],
                  max(dp[i][(j + 3) % 10], dp[i][j] + pr[pr.size() - 1] * 2 +
                                               pr[pr.size() - 2] +
                                               pr[pr.size() - 3]));
        } else if (pr.size() > 2) {
          dp[i + 1][j + 3] =
              max(dp[i + 1][j + 3],
                  max(dp[i][j + 3], dp[i][j] + pr[pr.size() - 1] +
                                        pr[pr.size() - 2] + pr[pr.size() - 3]));
        } else {
          dp[i + 1][(j + 3) % 10] =
              max(dp[i][(j + 3) % 10], dp[i + 1][(j + 3) % 10]);
        }
      }
    }
  }
  long long maksi = 0;
  for (int i = 0; i < 10; i++) {
    maksi = max(dp[n][i], maksi);
  }
  printf("%lld\n", maksi);
  return 0;
}
