#include <bits/stdc++.h>
using namespace std;
int const N = 1234567;
long long dp[N][12], n;
long long ccc[N][4];
long long ccc1[N][4];
long long getMaxOneCard(int t) { return ccc[t][1]; }
long long getMaxTwoCard(int t, bool sss) {
  if (sss) return ccc1[t][2];
  return ccc[t][2];
}
long long getMaxThreeCard(int t, bool sss) {
  if (sss) return ccc1[t][3];
  return ccc[t][3];
}
long long calc(int ind, int card) {
  if (ind == n) return 0;
  long long& rem = dp[ind][card];
  if (rem != -1) return rem;
  rem = calc(ind + 1, card);
  if (card + 1 == 10) {
    rem = max(rem, calc(ind + 1, 0) + getMaxOneCard(ind) * 2);
  } else {
    rem = max(rem, calc(ind + 1, card + 1) + getMaxOneCard(ind));
  }
  if (card + 2 >= 10) {
    rem = max(rem, calc(ind + 1, card + 2 - 10) + getMaxTwoCard(ind, 1));
  } else {
    rem = max(rem, calc(ind + 1, card + 2) + getMaxTwoCard(ind, 0));
  }
  if (card + 3 >= 10) {
    rem = max(rem, calc(ind + 1, card + 3 - 10) + getMaxThreeCard(ind, 1));
  } else {
    rem = max(rem, calc(ind + 1, card + 3) + getMaxThreeCard(ind, 0));
  }
  return rem;
}
int main() {
  memset(dp, -1, sizeof dp);
  cin >> n;
  for (int i = 0; i < n; i++) {
    int lol;
    scanf("%d", &lol);
    vector<long long> v1;
    ccc[i][1] = 0, ccc[i][2] = 0, ccc[i][3] = 0;
    ccc1[i][1] = 0, ccc1[i][2] = 0, ccc1[i][3] = 0;
    long long mx2 = 0, mx1 = 0;
    for (int x = 0; x < lol; x++) {
      int cost;
      long long dmg;
      scanf("%d%lld", &cost, &dmg);
      ccc[i][1] = max(ccc[i][1], dmg);
      if (cost == 2) mx2 = max(mx2, dmg);
      if (cost == 1) {
        v1.push_back(dmg);
        mx1 = max(mx1, dmg);
      }
    }
    sort(v1.rbegin(), v1.rend());
    if (v1.size() >= 2)
      for (int x = 0; x < min((int)v1.size(), 2); x++) {
        ccc[i][2] += v1[x];
        if (x == 0)
          ccc1[i][2] += v1[x] * 2;
        else
          ccc1[i][2] += v1[x];
      }
    if (mx1 != 0 && mx2 != 0) {
      ccc[i][2] = max(ccc[i][2], mx2 + mx1);
      ccc1[i][2] = max(ccc1[i][2], max(mx2, mx1) * 2 + min(mx2, mx1));
    }
    if (v1.size() >= 3)
      for (int x = 0; x < min((int)v1.size(), 3); x++) {
        ccc[i][3] += v1[x];
        if (x == 0)
          ccc1[i][3] += v1[x] * 2;
        else
          ccc1[i][3] += v1[x];
      }
  }
  cout << calc(0, 0);
  return 0;
}
