#include <bits/stdc++.h>
using namespace std;
long long n, k, cnt[200100][5][5], c, d, dp[200100][13];
vector<int> v;
long long Rec(int i, int j) {
  if (i == n) return 0;
  if (dp[i][j] != -1) return dp[i][j];
  long long Res1 = 0, Res2 = 0, Res3 = 0, Res4 = 0;
  if (cnt[i][3][0])
    Res1 = cnt[i][3][0] * (j == 9 ? 2 : 1) + Rec(i + 1, (j + 1) % 10);
  if (cnt[i][2][0])
    Res2 = cnt[i][2][0] * (j == 9 ? 2 : 1) + Rec(i + 1, (j + 1) % 10);
  if (cnt[i][2][0] && cnt[i][1][0])
    Res3 = max(cnt[i][2][0], cnt[i][1][0]) * (j == 8 || j == 9 ? 2 : 1) +
           min(cnt[i][2][0], cnt[i][1][0]) + Rec(i + 1, (j + 2) % 10);
  long long temp = 0, Max = 0;
  for (int k = 0; k < 3; k++) {
    if (cnt[i][1][k]) {
      Max = max(Max, cnt[i][1][k]);
      temp += cnt[i][1][k];
      Res4 = max(
          Res4, temp + ((j + k) >= 9 ? Max : 0) + Rec(i + 1, (j + k + 1) % 10));
    }
  }
  return dp[i][j] = max(max(Res1, Res2), max(Res3, Res4));
}
int main() {
  memset(dp, -1, sizeof(dp));
  cin >> n;
  for (int i = 0; i < n; i++) {
    scanf("%d", &k);
    for (int j = 0; j < k; j++) {
      cin >> c >> d;
      if (c == 1)
        v.push_back(d);
      else
        cnt[i][c][0] = max(cnt[i][c][0], d);
    }
    sort(v.begin(), v.end(), greater<int>());
    int idx = 0;
    for (auto x : v) cnt[i][1][idx++] = x;
    v.clear();
  }
  cout << Rec(0, 0);
  return 0;
}
