#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, k, curr;
  int c[200123], cost;
  long long damage, a[200123], d[200123], mx[4], dp[200123][10];
  vector<long long> v1;
  curr = 1;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> k;
    v1.clear();
    mx[1] = mx[2] = mx[3] = 0;
    for (int j = 0; j < k; j++) {
      cin >> cost >> damage;
      if (cost == 1) v1.push_back(damage);
      mx[cost] = max(mx[cost], damage);
    }
    sort(v1.begin(), v1.end(), greater<int>());
    a[curr] = max(mx[1], max(mx[2], mx[3]));
    c[curr] = 1;
    d[curr] = a[curr];
    curr++;
    if (v1.size() >= 1 || (mx[2] > 0 && v1.size() > 0)) {
      a[curr] = mx[1];
      if (v1.size() > 1 && v1[1] > mx[2]) {
        a[curr] += v1[1];
      } else {
        a[curr] += mx[2];
      }
      d[curr] = max(mx[1], mx[2]);
      c[curr] = 2;
      curr++;
    }
    if (v1.size() > 2) {
      a[curr] = v1[0] + v1[1] + v1[2];
      c[curr] = 3;
      d[curr] = mx[1];
      curr++;
    }
  }
  for (int i = 0; i < curr; i++)
    for (int j = 0; j < 10; j++) dp[i][j] = 0;
  for (int i = 1; i < curr; i++) {
    for (int j = 0; j < 10; j++) {
      dp[i][(j + c[i]) % 10] = dp[i - 1][(j + c[i]) % 10];
      if (j < i) {
        if ((j + c[i]) % 10 == 0)
          dp[i][(j + c[i]) % 10] =
              max(dp[i][(j + c[i]) % 10], dp[i - c[i]][j] + a[i] + d[i]);
        else
          dp[i][(j + c[i]) % 10] =
              max(dp[i][(j + c[i]) % 10], dp[i - c[i]][j] + a[i]);
      }
    }
  }
  long long ans = 0;
  for (int i = 0; i < 10; i++) ans = max(dp[curr - 1][i], ans);
  cout << ans << endl;
  return 0;
}
