#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
const int K = 1e1 + 2;
vector<int> one[N];
int two[N], three[N];
long long dp[N][K];
int n;
long long solve(int idx, int mod) {
  if (idx == n) return 0;
  if (dp[idx][mod] != -1) return dp[idx][mod];
  long long ans = solve(idx + 1, mod);
  mod = (mod + 1) % 10;
  if (three[idx]) {
    ans = max(
        ans, (mod == 0 ? (three[idx] << 1) : three[idx]) + solve(idx + 1, mod));
  }
  if (two[idx]) {
    if (one[idx].size()) {
      if (mod == 0 || mod == 9) {
        if (two[idx] > one[idx][0])
          ans = max(ans, (two[idx] << 1) + one[idx][0] +
                             solve(idx + 1, (mod + 1) % 10));
        else
          ans = max(ans, (one[idx][0] << 1) + two[idx] +
                             solve(idx + 1, (mod + 1) % 10));
      } else {
        ans = max(ans, two[idx] + one[idx][0] + solve(idx + 1, (mod + 1) % 10));
      }
    }
    ans =
        max(ans, (mod == 0 ? (two[idx] << 1) : two[idx]) + solve(idx + 1, mod));
  }
  if (one[idx].size() >= 1)
    ans = max(ans, (mod == 0 ? (one[idx][0] << 1) : one[idx][0]) +
                       solve(idx + 1, mod));
  if (one[idx].size() >= 2) {
    if (mod == 0 || mod == 9) {
      ans = max(ans, (one[idx][0] << 1) + one[idx][1] +
                         solve(idx + 1, (mod + 1) % 10));
    } else {
      ans =
          max(ans, one[idx][0] + one[idx][1] + solve(idx + 1, (mod + 1) % 10));
    }
  }
  if (one[idx].size() >= 3) {
    if (mod == 0 || mod == 9 || mod == 8) {
      ans = max(ans, (one[idx][0] << 1) + one[idx][1] + one[idx][2] +
                         solve(idx + 1, (mod + 2) % 10));
    } else {
      ans = max(ans, (one[idx][0] + one[idx][1] + one[idx][2] +
                      solve(idx + 1, (mod + 2) % 10)));
    }
  }
  return dp[idx][(mod - 1 + 10) % 10] = ans;
}
int main() {
  ios_base ::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n;
  for (int i = 0; i < n; i++) {
    int sz;
    cin >> sz;
    for (int j = 0; j < sz; j++) {
      int x, y;
      cin >> x >> y;
      if (x == 1) {
        one[i].push_back(y);
        if (one[i].size() == 4)
          sort(one[i].begin(), one[i].end(), greater<int>()), one[i].pop_back();
      } else if (x == 2) {
        two[i] = max(two[i], y);
      } else {
        three[i] = max(three[i], y);
      }
    }
  }
  for (int i = 0; i < N; i++)
    sort(one[i].begin(), one[i].end(), greater<int>());
  memset(dp, -1, sizeof(dp));
  cout << solve(0, 0);
  return 0;
}
