#include <bits/stdc++.h>
using namespace std;
double const EPS = 1e-12, PI = acos(-1);
const int N = 2e5 + 9, M = 1e7 + 9, OO = 1e9 + 7, MOD = 1e9 + 7,
          MOD2 = 998244353;
const long long inf = 1e18;
map<pair<int, int>, long long> mp;
vector<long long> v[N][4];
int n;
long long solve(int idx, int cards, long long parm) {
  if (idx > n) return 0ll;
  if (mp.find({idx, cards}) != mp.end()) return mp[{idx, cards}];
  long long ret = 0;
  ret = solve(idx + 1, cards, parm);
  for (int i = 1; i < 4; ++i) {
    if (v[idx][i].empty()) continue;
    long long total = 0;
    for (long long it : v[idx][i]) total += it;
    if ((cards + v[idx][i].size()) % 10 < cards % 10) {
      total -= v[idx][i][0];
      total += v[idx][i][0] * parm;
      ret =
          max(ret, solve(idx + 1, cards + v[idx][i].size(), parm * 2) + total);
    } else {
      ret = max(ret, solve(idx + 1, cards + v[idx][i].size(), parm) + total);
    }
  }
  return mp[{idx, cards}] = ret;
}
int main() {
  cout << fixed << setprecision(12), ios::sync_with_stdio(false),
      cin.tie(nullptr), cout.tie(nullptr);
  int c, d;
  cin >> n;
  for (int i = 1, k; i <= n; ++i) {
    vector<long long> vv[4];
    cin >> k;
    for (int j = 0; j < k; ++j) {
      cin >> c >> d;
      vv[c].push_back(d);
    }
    for (int j = 0; j < 4; ++j)
      sort(((vv[j]).begin()), ((vv[j]).end()), greater<long long>());
    for (int j = 0; j < (int)vv[1].size() && j < 3; ++j)
      v[i][1].push_back(vv[1][j]);
    for (int j = 0; j < (int)vv[2].size() && j < 1; ++j)
      v[i][2].push_back(vv[2][j]);
    if (!vv[1].empty()) v[i][2].push_back(vv[1][0]);
    for (int j = 0; j < (int)vv[3].size() && j < 1; ++j)
      v[i][3].push_back(vv[3][j]);
  }
  cout << solve(1, 0, 2);
  return 0;
}
