#include <bits/stdc++.h>
const long long INF = 100000000000000000ll;
long long dp[200005][11];
int n;
std::vector<int> cards[3];
std::vector<std::vector<int> > moves = {{1, 0, 0}, {2, 0, 0}, {3, 0, 0},
                                        {1, 1, 0}, {0, 1, 0}, {0, 0, 1}};
int main() {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(0);
  std::cin >> n;
  for (int i = 0; i < (200005); ++i)
    for (int j = 0; j < (11); ++j) dp[i][j] = -INF;
  dp[0][0] = 0;
  for (int i = (1); i < (n + 1); ++i) {
    for (int j = 0; j < (3); ++j) cards[j].clear();
    int k;
    std::cin >> k;
    for (int j = 0; j < (k); ++j) {
      int c, d;
      std::cin >> c >> d;
      cards[c - 1].push_back(d);
    }
    for (int j = 0; j < (3); ++j)
      std::sort(cards[j].begin(), cards[j].end(), std::greater<int>());
    for (auto &mv : moves) {
      bool bad = false;
      std::vector<int> cr;
      long long sum = 0;
      long long max = 0;
      for (int j = 0; j < (3); ++j) {
        if ((int)(cards[j]).size() < mv[j]) {
          bad = true;
          break;
        }
        for (int l = 0; l < (mv[j]); ++l) {
          cr.push_back(cards[j][l]);
          max = std::max(max, 1ll * cr.back());
          sum += cr.back();
        }
      }
      if (bad) continue;
      std::sort(cr.begin(), cr.end());
      for (int j = 0; j < (10); ++j) {
        int last = j - (int)(cr).size();
        if (last < 0) last += 10;
        if (last > j) {
          dp[i][j] = std::max(dp[i][j], dp[i - 1][last] + sum + max);
        } else {
          dp[i][j] = std::max(dp[i][j], dp[i - 1][last] + sum);
        }
      }
    }
  }
  long long max = 0;
  for (int i = 0; i < (10); ++i) max = std::max(max, dp[n][i]);
  std::cout << max;
  return 0;
}
