#include <bits/stdc++.h>
using namespace std;
const int Maxn = 200005;
int n, len[Maxn];
long long ans;
vector<long long> a[Maxn][4];
long long dp[Maxn][10], f[4][2];
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%d", len + i);
    for (int j = 0; j < len[i]; ++j) {
      int x;
      long long y;
      scanf("%d %lld", &x, &y);
      a[i][x].push_back(y);
    }
    for (int j = 1; j < 4; ++j) {
      sort(a[i][j].rbegin(), a[i][j].rend());
      while ((int)a[i][j].size() * j > 3) a[i][j].pop_back();
    }
  }
  memset(dp, -63, sizeof(dp));
  dp[0][0] = 0;
  for (int i = 1; i <= n; ++i) {
    memset(f, -63, sizeof(f));
    f[0][0] = f[0][1] = 0;
    if ((int)a[i][1].size() == 3) {
      f[3][0] = a[i][1][0] + a[i][1][1] + a[i][1][2];
      f[3][1] = a[i][1][0] * 2 + a[i][1][1] + a[i][1][2];
    } else {
      f[3][0] = f[3][1] = 0;
    }
    for (int j = 1; j < 4; ++j)
      for (auto &x : a[i][j])
        f[1][0] = max(f[1][0], x), f[1][1] = max(f[1][1], x * 2);
    for (auto &x : a[i][1])
      for (auto &y : a[i][2])
        f[2][0] = max(f[2][0], x + y),
        f[2][1] = max(f[2][1], max(x, y) * 2 + min(x, y));
    if ((int)a[i][1].size() >= 2) {
      for (int j1 = 0; j1 < (int)a[i][1].size(); ++j1)
        for (int j2 = j1 + 1; j2 < (int)a[i][1].size(); ++j2) {
          f[2][0] = max(f[2][0], a[i][1][j1] + a[i][1][j2]);
          f[2][1] = max(f[2][1], a[i][1][j1] * 2 + a[i][1][j2]);
        }
    }
    for (int j = 0; j < 10; ++j)
      for (int k = 0; k <= 3; ++k) {
        bool flag = j + k >= 10;
        long long now = dp[i - 1][j] + f[k][flag];
        dp[i][(j + k) % 10] = max(dp[i][(j + k) % 10], now);
      }
  }
  for (int i = n; ~i; --i)
    for (int j = 9; ~j; --j) ans = max(ans, dp[i][j]);
  printf("%lld\n", ans);
  return 0;
}
