#include <bits/stdc++.h>
using namespace std;
int const MAX_N = 2e5 + 10;
int64_t DP[MAX_N][13];
int n;
void Solve() {
  cin >> n;
  for (int i = 0; i <= n; i++)
    for (int j = 1; j <= 9; j++) DP[i][j] = -1e9;
  DP[0][0] = 0;
  for (int i = 1; i <= n; i++) {
    vector<int> listCard[4];
    int k;
    cin >> k;
    for (int j = 1; j <= k; j++) {
      int c, d;
      cin >> c >> d;
      listCard[c].push_back(d);
    }
    for (int j = 1; j <= 3; j++)
      sort(listCard[j].begin(), listCard[j].end(), greater<int>());
    for (int j = 0; j <= 9; j++) {
      if (DP[i - 1][j] != -1e9) {
        for (int cnt = 1; cnt <= 3; cnt++) {
          for (int type = 1; type <= 3; type++) {
            if (listCard[type].size() < cnt) continue;
            int64_t ma = 0, cost = 0, d = 0, total = 0;
            while (cost + type <= 3 && d < cnt) {
              total += listCard[type][d];
              ma = max(ma, (int64_t)listCard[type][d]);
              d++;
              cost += type;
            }
            if (d == cnt)
              DP[i][(j + cnt) % 10] =
                  max(DP[i][(j + cnt) % 10],
                      DP[i - 1][j] + total + ((j + cnt >= 10) ? ma : 0));
          }
        }
        if (listCard[1].size() > 0 && listCard[2].size() > 0) {
          int ma = max(listCard[1][0], listCard[2][0]);
          int total = listCard[1][0] + listCard[2][0];
          DP[i][(j + 2) % 10] =
              max(DP[i][(j + 2) % 10],
                  DP[i - 1][j] + total + (j + 2 >= 10 ? ma : 0));
        }
      }
    }
  }
  int64_t result = 0;
  for (int i = 0; i <= 9; i++) result = max(result, DP[n][i]);
  cout << result;
}
int main() { Solve(); }
