#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const long long INF = 1e18;
const int maxn = 2 * 1e5 + 5;
vector<pair<long long, long long> > a[maxn];
long long dp[maxn][10];
int get_in(int x, int y) {
  if (x - y >= 0) return x - y;
  return x + 10 - y;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < 10; j++) dp[i][j] = -INF;
  dp[0][0] = 0;
  for (int i = 0; i < n; i++) {
    int k;
    cin >> k;
    a[i].resize(4);
    vector<long long> ones;
    long long max_2 = -INF, max_3 = -INF;
    for (int j = 0; j < k; j++) {
      int c;
      long long d;
      cin >> c >> d;
      if (c == 1) ones.push_back(d);
      if (c == 2 && d > max_2) max_2 = d;
      if (c == 3 && d > max_3) max_3 = d;
    }
    sort(ones.rbegin(), ones.rend());
    vector<pair<long long, int> > cards;
    for (int j = 0; j < min(3, (int)ones.size()); j++)
      cards.push_back({ones[j], 1});
    if (max_2 != -INF) cards.push_back({max_2, 2});
    if (max_3 != -INF) cards.push_back({max_3, 3});
    int size = (int)cards.size();
    for (int j = 0; j < size; j++)
      if (cards[j].first > a[i][1].first)
        a[i][1] = {cards[j].first, 2 * cards[j].first};
    for (int j = 0; j < size; j++) {
      for (int z = 0; z < size; z++) {
        if (z == j) continue;
        long long val = cards[j].first + cards[z].first;
        if (cards[j].second + cards[z].second <= 3 && val > a[i][2].first)
          a[i][2] = {val, val + max(cards[j].first, cards[z].first)};
      }
    }
    for (int j = 0; j < size; j++) {
      for (int z = 0; z < size; z++) {
        if (z == j) continue;
        for (int x = 0; x < size; x++) {
          if (x == j || x == z) continue;
          long long val = cards[j].first + cards[z].first + cards[x].first;
          if (cards[j].second + cards[z].second + cards[x].second <= 3 &&
              val > a[i][3].first)
            a[i][3] = {val, val + max(max(cards[j].first, cards[z].first),
                                      cards[x].first)};
        }
      }
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 10; j++) {
      for (int z = 0; z < 4; z++) {
        int in = (j + z) % 10;
        long long x = (j + z >= 10) ? a[i][z].second : a[i][z].first;
        dp[i + 1][in] = max(dp[i + 1][in], dp[i][j] + x);
      }
    }
  }
  long long ans = 0;
  for (int j = 0; j < 10; j++) ans = max(ans, dp[n][j]);
  cout << ans << "\n";
  return 0;
}
