#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  vector<long long> prev_state(11, 0LL);
  for (int i = 0; i < n; i++) {
    int k;
    cin >> k;
    int dam3 = 0, dam2 = 0;
    vector<int> dam1;
    for (int j = 0; j < k; j++) {
      int c, d;
      cin >> c >> d;
      if (c == 3) {
        dam3 = max(dam3, d);
      } else if (c == 2) {
        dam2 = max(dam2, d);
      } else {
        dam1.push_back(d);
      }
    }
    sort(dam1.rbegin(), dam1.rend());
    if (i == 0) {
      prev_state[1] = dam3;
      if ((int)dam1.size() > 0) {
        prev_state[2] = dam2 + dam1[0];
      }
      if ((int)dam1.size() > 2) {
        prev_state[3] = dam1[0] + dam1[1] + dam1[2];
      }
    } else {
      vector<long long> new_st(11, 0);
      long long ch1 = dam3, ch2 = 0, ch3 = 0;
      if ((int)dam1.size() > 0) {
        ch2 = dam2 + dam1[0];
      }
      if ((int)dam1.size() > 2) {
        ch3 = dam1[0] + dam1[1] + dam1[2];
      }
      for (int j = 1; j <= 10; j++) {
        if (prev_state[j] != 0) {
          int nxpos;
          nxpos = j + 1;
          if (nxpos > 10) nxpos -= 10;
          if (dam3 > 0) {
            long long temp_dam = prev_state[j] + dam3;
            if (nxpos == 10) temp_dam += dam3;
            new_st[nxpos] = max(new_st[nxpos], temp_dam);
          }
          if (dam2 > 0) {
            long long temp_dam = prev_state[j] + dam2;
            if (nxpos == 10) temp_dam += dam2;
            new_st[nxpos] = max(new_st[nxpos], temp_dam);
          }
          if ((int)dam1.size() > 0) {
            long long temp_dam = prev_state[j] + dam1[0];
            if (nxpos == 10) temp_dam += dam1[0];
            new_st[nxpos] = max(new_st[nxpos], temp_dam);
          }
          nxpos = j + 2;
          if (nxpos > 10) nxpos -= 10;
          if (dam2 > 0 && (int)dam1.size() > 0) {
            long long temp_dam = prev_state[j] + dam2 + dam1[0];
            if (nxpos == 1 || nxpos == 10) temp_dam += max(dam1[0], dam2);
            new_st[nxpos] = max(new_st[nxpos], temp_dam);
          }
          if ((int)dam1.size() > 1) {
            long long temp_dam = prev_state[j] + dam1[0] + dam1[1];
            if (nxpos == 1 || nxpos == 10) temp_dam += dam1[0];
            new_st[nxpos] = max(new_st[nxpos], temp_dam);
          }
          if ((int)dam1.size() > 2) {
            long long temp_dam = prev_state[j] + dam1[0] + dam1[1] + dam1[2];
            if (nxpos == 2 || nxpos == 1 || nxpos == 10) temp_dam += dam1[0];
            new_st[nxpos] = max(new_st[nxpos], temp_dam);
          }
        }
      }
      for (int j = 1; j <= 10; j++) prev_state[j] = new_st[j];
    }
  }
  long long ans = 0;
  for (int i = 1; i <= 10; i++) {
    ans = max(ans, prev_state[i]);
  }
  cout << ans;
  return 0;
}
