#include <bits/stdc++.h>
using namespace std;
const int N = 3e6 + 1;
const int NN = 1e3 + 1;
const long long MOD = 1e9 + 7;
const long long oo = 1e18 + 7;
void solve() {
  int n;
  cin >> n;
  vector<vector<pair<long long, long long> > > s(
      n + 1, vector<pair<long long, long long> >(4, make_pair(0, 0)));
  for (int i = 1; i <= n; i++) {
    int k;
    cin >> k;
    vector<vector<long long> > p(4);
    for (int j = 1; j <= k; j++) {
      int c, d;
      cin >> c >> d;
      p[c].push_back(d);
    }
    for (int j = 0; j < 4; j++) {
      sort((p[j]).begin(), (p[j]).end(), greater<long long>());
    }
    for (int j = 1; j < 4; j++) {
      if ((int)(p[j]).size() > 0) {
        s[i][1].first = max(p[j][0], s[i][1].first);
      }
    }
    s[i][1].second = s[i][1].first;
    if (k >= 2) {
      if ((int)(p[1]).size() >= 2) {
        s[i][2].first = max(s[i][2].first, p[1][0] + p[1][1]);
        if (s[i][2].first == p[1][0] + p[1][1]) {
          s[i][2].second = max(p[1][0], p[1][1]);
        }
      }
      if ((int)(p[1]).size() > 0 && (int)(p[2]).size() > 0) {
        s[i][2].first = max(s[i][2].first, p[1][0] + p[2][0]);
        if (s[i][2].first == p[1][0] + p[2][0]) {
          s[i][2].second = max(p[1][0], p[2][0]);
        }
      }
    }
    if ((int)(p[1]).size() >= 3) {
      s[i][3].first = p[1][0] + p[1][1] + p[1][2];
      s[i][3].second = max(p[1][0], max(p[1][1], p[1][2]));
    }
  }
  vector<vector<long long> > dp(n + 1, vector<long long>(10));
  for (int i = 1; i <= 3; i++) {
    dp[1][i] = s[1][i].first;
  }
  for (int i = 2; i <= n; i++) {
    for (int j = 0; j < 10; j++) {
      if (dp[i - 1][j] > 0) {
        for (int k = 0; k < 4; k++) {
          if (s[i][k].first) {
            long long t = dp[i - 1][j] + s[i][k].first;
            if (j + k >= 10) {
              t += s[i][k].second;
            }
            dp[i][(j + k) % 10] = max(dp[i][(j + k) % 10], t);
          }
        }
      }
    }
  }
  long long res = 0;
  for (int i = 0; i < 10; i++) {
    res = max(res, dp[n][i]);
  }
  cout << res;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(nullptr);
  cout.tie(nullptr);
  cout.precision(10);
  solve();
  cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
}
