#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  long long m[10] = {};
  for (int i = 0; i < n; ++i) {
    int k;
    cin >> k;
    long long item1[3] = {}, item2 = 0, item3 = 0;
    for (int j = 0; j < k; ++j) {
      int c;
      long long d;
      cin >> c >> d;
      if (c == 1) {
        item1[0] = max(item1[0], d);
        sort(item1, item1 + 3);
      } else if (c == 2)
        item2 = max(item2, d);
      else
        item3 = max(item3, d);
    }
    int s1 = 0;
    for (auto it : item1)
      if (it) s1++;
    vector<long long> cards;
    if (s1 >= 1 || item2 || item3)
      cards.push_back(max(item2, max(item3, item1[2])));
    if (s1 >= 2 || (item2 && s1 >= 1))
      cards.push_back(max(item2 + item1[2], item1[2] + item1[1]));
    if (s1 == 3) cards.push_back(item1[0] + item1[1] + item1[2]);
    int c = cards.size();
    long long temp[10];
    copy(m, m + 10, temp);
    if (c == 1) {
      temp[1] = max(m[1], m[0] + cards[0]);
      for (int l = 2; l <= 9; ++l) {
        if (m[l - 1]) temp[l] = max(m[l], m[l - 1] + cards[0]);
      }
      if (m[9]) temp[0] = max(m[0], m[9] + 2 * cards[0]);
    }
    if (c == 2) {
      temp[2] = max(m[2], m[0] + cards[1]);
      for (int l = 1; l <= 9; ++l) {
        if (l == 2) continue;
        if (m[(10 + l - 2) % 10])
          temp[l] = max(temp[l], m[(10 + l - 2) % 10] + cards[1]);
      }
      if (m[8]) temp[0] = max(temp[0], m[8] + max(item2, item1[2]) + cards[1]);
    }
    if (c == 3) {
      temp[3] = max(m[3], m[0] + cards[2]);
      for (int l = 1; l <= 9; ++l) {
        if (l == 3) continue;
        if (m[(10 + l - 3) % 10])
          temp[l] = max(temp[l], m[(10 + l - 3) % 10] + cards[2]);
      }
      if (m[7]) temp[0] = max(temp[0], m[7] + item1[2] + cards[2]);
    }
    copy(temp, temp + 10, m);
  }
  long long mV = 0;
  for (auto x : m) {
    mV = max(mV, x);
  }
  cout << mV;
  return 0;
}
